<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <title></title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <style type="text/css">
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%; }
body .hll { background-color: #ffffcc }
body  { background: #f8f8f8; }
body .c { color: #408080; font-style: italic } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
body .k { color: #008000; font-weight: bold } /* Keyword */
body .o { color: #666666 } /* Operator */
body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
body .cp { color: #BC7A00 } /* Comment.Preproc */
body .c1 { color: #408080; font-style: italic } /* Comment.Single */
body .cs { color: #408080; font-style: italic } /* Comment.Special */
body .gd { color: #A00000 } /* Generic.Deleted */
body .ge { font-style: italic } /* Generic.Emph */
body .gr { color: #FF0000 } /* Generic.Error */
body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
body .gi { color: #00A000 } /* Generic.Inserted */
body .go { color: #808080 } /* Generic.Output */
body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
body .gt { color: #0040D0 } /* Generic.Traceback */
body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
body .kp { color: #008000 } /* Keyword.Pseudo */
body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
body .kt { color: #B00040 } /* Keyword.Type */
body .m { color: #666666 } /* Literal.Number */
body .s { color: #BA2121 } /* Literal.String */
body .na { color: #7D9029 } /* Name.Attribute */
body .nb { color: #008000 } /* Name.Builtin */
body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
body .no { color: #880000 } /* Name.Constant */
body .nd { color: #AA22FF } /* Name.Decorator */
body .ni { color: #999999; font-weight: bold } /* Name.Entity */
body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
body .nf { color: #0000FF } /* Name.Function */
body .nl { color: #A0A000 } /* Name.Label */
body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
body .nt { color: #008000; font-weight: bold } /* Name.Tag */
body .nv { color: #19177C } /* Name.Variable */
body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
body .w { color: #bbbbbb } /* Text.Whitespace */
body .mf { color: #666666 } /* Literal.Number.Float */
body .mh { color: #666666 } /* Literal.Number.Hex */
body .mi { color: #666666 } /* Literal.Number.Integer */
body .mo { color: #666666 } /* Literal.Number.Oct */
body .sb { color: #BA2121 } /* Literal.String.Backtick */
body .sc { color: #BA2121 } /* Literal.String.Char */
body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
body .s2 { color: #BA2121 } /* Literal.String.Double */
body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
body .sh { color: #BA2121 } /* Literal.String.Heredoc */
body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
body .sx { color: #008000 } /* Literal.String.Other */
body .sr { color: #BB6688 } /* Literal.String.Regex */
body .s1 { color: #BA2121 } /* Literal.String.Single */
body .ss { color: #19177C } /* Literal.String.Symbol */
body .bp { color: #008000 } /* Name.Builtin.Pseudo */
body .vc { color: #19177C } /* Name.Variable.Class */
body .vg { color: #19177C } /* Name.Variable.Global */
body .vi { color: #19177C } /* Name.Variable.Instance */
body .il { color: #666666 } /* Literal.Number.Integer.Long */

  </style>
</head>
<body>
<h2></h2>

<div class="highlight"><pre><span class="c1">// vim: tabstop=4:shiftwidth=4:expandtab </span>
<span class="cm">/*</span>
<span class="cm"> * fbutils helper library for pythons fbpy module. Draws stuff in the</span>
<span class="cm"> * Linux framebuffer.</span>
<span class="cm"> * Copyright (C) 2014  Marcell Marosvolgyi aka noisegate</span>
<span class="cm"> * </span>
<span class="cm"> * This program is free software; you can redistribute it and/or</span>
<span class="cm"> * modify it under the terms of the GNU General Public License</span>
<span class="cm"> * as published by the Free Software Foundation; either version 2</span>
<span class="cm"> * of the License, or (at your option) any later version.</span>
<span class="cm"> * </span>
<span class="cm"> * This program is distributed in the hope that it will be useful,</span>
<span class="cm"> *      but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="cm"> *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="cm"> *      GNU General Public License for more details.</span>
<span class="cm"> * </span>
<span class="cm"> *      You should have received a copy of the GNU General Public License</span>
<span class="cm"> *      along with this program; if not, write to the Free Software</span>
<span class="cm"> *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.</span>
<span class="cm"> *      </span>
<span class="cm"> */</span>

<span class="cp">#include &lt;unistd.h&gt;</span>
<span class="cp">#include &lt;stdio.h&gt;</span>
<span class="cp">#include &lt;stdlib.h&gt;</span>
<span class="cp">#include &lt;string.h&gt;</span>
<span class="cp">#include &lt;fcntl.h&gt;</span>
<span class="cp">#include &lt;linux/fb.h&gt;</span>
<span class="cp">#include &lt;sys/mman.h&gt;</span>
<span class="cp">#include &lt;sys/ioctl.h&gt;</span>
<span class="cp">#include &lt;math.h&gt;</span>
<span class="cp">#include &quot;font.h&quot;</span>
<span class="cp">#include &lt;png.h&gt;</span>
<span class="cp">#include &quot;test.h&quot;</span>
<span class="cp">#include &quot;fbutils.h&quot;</span>
<span class="cp">#include &lt;time.h&gt;</span>

<span class="cp">//TODO: I really have to clean this</span>
<span class="cp">//shit. It&#39;s getting way too GLOBAL</span>

<span class="cp">#ifndef FB_VBLANK_HAVE_STICKY</span>
<span class="cp">#define FB_VBLANK_HAVE_STICKY 0</span>
<span class="cp">#endif</span>

<span class="cp">#ifndef FB_VBLANK_STICKY</span>
<span class="cp">#define FB_VBLANK_STICKY 0</span>
<span class="cp">#endif</span>

<span class="cp">#ifdef FBIOGET_VBLANK</span>
<span class="k">static</span> <span class="kt">int</span> <span class="n">vblank_flags</span><span class="p">;</span>                  <span class="cm">/* supports retrace detection? */</span>
<span class="cp">#endif</span>

<span class="cp">#define TO_BLOCK 1</span>
<span class="cp">#define FROM_BLOCK 0</span>

<span class="cp">#define FBUTILS_ERROR_OUT_OF_MEMORY -2</span>
<span class="cp">#define FBUTILS_ERROR_GENERAL -3</span>
<span class="cp">#define FBUTILS_ERROR_NOT_YET_IMPLEMENTED -1</span>

<span class="cp">#define max255(x) (((x)&gt;255)?255:(x))</span>
<span class="cp">#define MAXBLUR 10</span>
<span class="cp">#define MAXBLURHALF 5</span>

<span class="cp">#define TON start=clock()</span>
<span class="cp">#define TOFF end=clock();printf(&quot;elapsed %1d.&quot;,(double)(end-start))</span>

<span class="cp">#define BPP 4                           </span><span class="c1">//well, bytes per pixel</span>

<span class="cp">#define xaxis 0</span>
<span class="cp">#define yaxis 9</span>
<span class="cp">#define zaxis 18</span>

<span class="kt">int</span> <span class="n">fbfd</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">fb_var_screeninfo</span> <span class="n">orig_vinfo</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">fb_var_screeninfo</span> <span class="n">vinfo</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">fb_fix_screeninfo</span> <span class="n">finfo</span><span class="p">;</span>
<span class="kt">long</span> <span class="kt">int</span> <span class="n">screensize</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">fbp</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">tmp_buf</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">dummy_buf</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="kt">char</span> <span class="n">skipper</span><span class="p">;</span>				<span class="c1">//for linestyle, global 4 continuity</span>
<span class="kt">int</span> <span class="n">blur</span><span class="p">;</span>				    <span class="c1">//makes plot dots blurred</span>
<span class="kt">int</span> <span class="n">blurrad</span><span class="p">;</span>                <span class="c1">//gauss width or somethin&#39;</span>
<span class="kt">int</span> <span class="n">sigma</span><span class="p">;</span>                  <span class="c1">//sigma of the Gaussian</span>
<span class="kt">char</span> <span class="n">linestyle</span><span class="p">;</span>             <span class="c1">//0 = solid, 1=dash, 2=dot</span>
<span class="kt">int</span> <span class="n">bytes_per_pixel</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">Ox</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">Oy</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">width</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">width4</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">height</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">cur_page</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">kern</span><span class="p">[</span><span class="n">MAXBLUR</span><span class="p">][</span><span class="n">MAXBLUR</span><span class="p">];</span>
<span class="kt">clock_t</span> <span class="n">end</span><span class="p">,</span><span class="n">start</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">Color</span> <span class="o">*</span><span class="n">currentcolor</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">Trafo</span> <span class="o">*</span><span class="n">currenttrafo</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">Trafo3</span> <span class="o">*</span><span class="n">currenttrafo3</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="kt">int</span> <span class="p">(</span><span class="o">*</span><span class="n">pPlot</span><span class="p">)</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">);</span>
<span class="kt">int</span> <span class="p">(</span><span class="o">*</span><span class="n">lintrafo</span><span class="p">[</span><span class="mi">5</span><span class="p">])(</span><span class="kt">int</span> <span class="o">*</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="p">)</span><span class="o">=</span><span class="p">{</span><span class="nb">NULL</span><span class="p">};</span>

<span class="kt">char</span> <span class="n">gauzz</span><span class="p">[]</span><span class="o">=</span><span class="p">{</span>
		<span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span> <span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span> <span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span>
		<span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span> <span class="mh">0xff</span><span class="p">,</span><span class="mh">0xff</span><span class="p">,</span><span class="mh">0xff</span><span class="p">,</span><span class="mh">0xff</span><span class="p">,</span> <span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span>
		<span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span> <span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span><span class="mh">0x20</span><span class="p">,</span> <span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">,</span><span class="mh">0x10</span><span class="p">};</span>

<span class="kt">int</span> <span class="nf">setup</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">//http://stackoverflow.com/questions/4996777/paint-pixels-to-screen-via-linux-framebuffer</span>
    <span class="c1">//modified</span>

    <span class="n">fbfd</span> <span class="o">=</span> <span class="n">open</span><span class="p">(</span><span class="s">&quot;/dev/fb0&quot;</span><span class="p">,</span> <span class="n">O_RDWR</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">fbfd</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error: cannot open framebuffer device.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">FBUTILS_ERROR_GENERAL</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="c1">//printf(&quot;The framebuffer device was opened successfully.\n&quot;);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VSCREENINFO</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vinfo</span><span class="p">))</span> <span class="p">{</span>
      <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error reading variable information.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
      <span class="k">return</span> <span class="n">FBUTILS_ERROR_GENERAL</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="c1">//printf(&quot;Original %dx%d, %dbpp\n&quot;, vinfo.xres, vinfo.yres, </span>
	<span class="c1">//   vinfo.bits_per_pixel );</span>

    <span class="c1">// Store for reset (copy vinfo to vinfo_orig)</span>
    <span class="c1">// should not be here, but/??</span>
    <span class="c1">// maybe before update??? trying...</span>
    <span class="c1">// memcpy(&amp;orig_vinfo, &amp;vinfo, sizeof(struct fb_var_screeninfo));</span>

    <span class="c1">// Change variable info</span>
    <span class="n">vinfo</span><span class="p">.</span><span class="n">bits_per_pixel</span> <span class="o">=</span> <span class="mi">32</span><span class="p">;</span>
    <span class="c1">//vinfo.xres = 1366;</span>
    <span class="c1">//vinfo.yres = 768;</span>
    <span class="n">vinfo</span><span class="p">.</span><span class="n">xres_virtual</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">xres</span><span class="p">;</span>
    <span class="n">vinfo</span><span class="p">.</span><span class="n">yres_virtual</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span> <span class="o">*</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">// double the physical</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOPUT_VSCREENINFO</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vinfo</span><span class="p">))</span> <span class="p">{</span>
      <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error setting variable information.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
      <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">bytes_per_pixel</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">bits_per_pixel</span> <span class="o">/</span> <span class="mi">4</span><span class="p">;</span>
    <span class="c1">// Get fixed screen information</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_FSCREENINFO</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">finfo</span><span class="p">))</span> <span class="p">{</span>
      <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error reading fixed information.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
      <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="c1">// map fb to user mem </span>
    <span class="c1">// TODO:</span>
    <span class="c1">//finfo line_length gives 5504</span>
    <span class="c1">//5504/4 = 1376 not 1366 hmmm</span>
    <span class="c1">//</span>
    <span class="c1">//added 4 here!!! &#39;cos bits per pixel is 32 not 8</span>
    <span class="c1">//screensize = 4*vinfo.xres * vinfo.yres;</span>
    <span class="c1">//doubling this because of the flipping</span>
    <span class="c1">//use virtual is already twice the physical</span>
    <span class="c1">//screensize = 4 * vinfo.xres_virtual * vinfo.yres_virtual;</span>
    <span class="c1">//screensize = finfo.line_length * vinfo.yres_virtual;</span>
    <span class="c1">//screensize = BPP * vinfo.xres * vinfo.yres;</span>
    <span class="n">screensize</span> <span class="o">=</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span> <span class="o">*</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span><span class="p">;</span>
    <span class="n">fbp</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">mmap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> 
		<span class="n">screensize</span><span class="p">,</span> 
		<span class="n">PROT_READ</span> <span class="o">|</span> <span class="n">PROT_WRITE</span><span class="p">,</span> 
		<span class="n">MAP_SHARED</span><span class="p">,</span> 
		<span class="n">fbfd</span><span class="p">,</span> 
		<span class="mi">0</span><span class="p">);</span>
    <span class="n">Ox</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">Oy</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">width</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">xres</span><span class="p">;</span>
    <span class="n">width4</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">xres</span> <span class="o">*</span> <span class="n">BPP</span><span class="p">;</span><span class="c1">//yih</span>
    <span class="n">height</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span><span class="p">;</span>

    <span class="c1">//printf (&quot;screensize %i \n&quot;, screensize);</span>
    <span class="c1">//printf (&quot;screensize %i \n&quot;, finfo.smem_len);</span>


    <span class="c1">//allocate temp memory stuff....</span>
    <span class="c1">//</span>
    <span class="n">tmp_buf</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">((</span><span class="kt">size_t</span><span class="p">)</span><span class="n">screensize</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">tmp_buf</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">){</span>
		<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Out of memory...</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
		<span class="k">return</span> <span class="n">FBUTILS_ERROR_OUT_OF_MEMORY</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">dummy_buf</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">((</span><span class="kt">size_t</span><span class="p">)</span><span class="n">screensize</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">dummy_buf</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">){</span>
		<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Out of memory...</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
		<span class="k">return</span> <span class="n">FBUTILS_ERROR_OUT_OF_MEMORY</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">memcpy</span><span class="p">(</span><span class="n">tmp_buf</span><span class="p">,</span> <span class="n">fbp</span><span class="p">,</span> <span class="n">screensize</span><span class="p">);</span>
    <span class="n">memcpy</span><span class="p">(</span><span class="n">dummy_buf</span><span class="p">,</span> <span class="n">fbp</span><span class="p">,</span> <span class="n">screensize</span><span class="p">);</span>
    <span class="c1">//memset(tmp_buf, (int)0, screensize+1);</span>
    <span class="c1">//printf(&quot;screensize %i\n&quot;, screensize);</span>
    <span class="c1">//printf(&quot;size of tmp buf: %i \n&quot;, sizeof(tmp_buf));</span>
    <span class="c1">//use the fast plotroutine by default.</span>
    <span class="c1">//</span>
    <span class="n">pPlot</span> <span class="o">=</span> <span class="n">plot_</span><span class="p">;</span>

    <span class="n">currentcolor</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">Color</span><span class="p">));</span>
    <span class="n">currenttrafo</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">Trafo</span><span class="p">));</span>
    <span class="n">currenttrafo3</span><span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">Trafo3</span><span class="p">));</span>

    <span class="n">kernel</span><span class="p">();</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;yres = %i&quot;</span><span class="p">,</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span><span class="p">);</span><span class="n">fflush</span><span class="p">(</span><span class="n">stdout</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">swappage</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">)</span> 
<span class="p">{</span>
	<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">keepcurrent</span><span class="p">()</span>
<span class="p">{</span>
	<span class="n">memcpy</span><span class="p">(</span><span class="n">tmp_buf</span><span class="p">,</span> <span class="n">fbp</span><span class="p">,</span> <span class="n">screensize</span><span class="p">);</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">setblurrad</span><span class="p">(</span><span class="kt">int</span> <span class="n">r</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">blurrad</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">settrafo</span> <span class="p">(</span><span class="k">struct</span> <span class="n">Trafo</span> <span class="o">*</span><span class="n">mytrafo</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m11</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">m11</span><span class="p">;</span>
    <span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m12</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">m12</span><span class="p">;</span>
    <span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m21</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">m21</span><span class="p">;</span>
    <span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m22</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">m22</span><span class="p">;</span>
    <span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">unity</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">unity</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">settrafo3</span> <span class="p">(</span><span class="k">struct</span> <span class="n">Trafo3</span> <span class="o">*</span><span class="n">mytrafo</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetax</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">tetax</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetay</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">tetay</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetaz</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">tetaz</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetax</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">ctetax</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetay</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">ctetay</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetaz</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">ctetaz</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ex</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">ex</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ey</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">ey</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ez</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">ez</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">cx</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">cx</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">cy</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">cy</span><span class="p">;</span>
    <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">cz</span> <span class="o">=</span> <span class="n">mytrafo</span><span class="o">-&gt;</span><span class="n">cz</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">setwinparams</span><span class="p">(</span>   <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="n">width_</span><span class="p">,</span> <span class="kt">int</span> <span class="n">height_</span><span class="p">,</span> 
                    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">r</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">g</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">b</span><span class="p">,</span>
                    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">a</span><span class="p">,</span>
                    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">linestyle_</span><span class="p">,</span> 
                    <span class="kt">int</span> <span class="n">blur_</span><span class="p">,</span> <span class="kt">int</span> <span class="n">blurrad_</span><span class="p">,</span> <span class="kt">int</span> <span class="n">sigma_</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">//this function should really be invoked</span>
    <span class="c1">//each time a fb method is called</span>
    <span class="c1">//maybe move color and style here too</span>
    <span class="c1">//TODO: here we need a struct, soooooo</span>
    <span class="c1">//many args are UGLY</span>
    <span class="n">Ox</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>
    <span class="n">Oy</span> <span class="o">=</span> <span class="n">y</span><span class="p">;</span>
    <span class="n">width</span> <span class="o">=</span> <span class="n">width_</span><span class="p">;</span>
    <span class="n">width4</span> <span class="o">=</span> <span class="n">width</span><span class="o">*</span><span class="n">BPP</span><span class="p">;</span>
    <span class="n">height</span> <span class="o">=</span> <span class="n">height_</span><span class="p">;</span>

    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span> <span class="o">=</span> <span class="n">g</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span>

    <span class="n">linestyle</span> <span class="o">=</span> <span class="n">linestyle_</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">blur_</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="n">pPlot</span> <span class="o">=</span> <span class="n">plot_</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">blur_</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="n">pPlot</span> <span class="o">=</span> <span class="n">plotalpha_</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">blur_</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="n">pPlot</span> <span class="o">=</span> <span class="n">plotblurred_</span><span class="p">;</span>
    <span class="n">blurrad</span> <span class="o">=</span> <span class="n">blurrad_</span><span class="o">&gt;</span><span class="mi">5</span><span class="o">?</span><span class="mi">5</span><span class="o">:</span><span class="n">blurrad_</span><span class="p">;</span>        <span class="c1">//decided to put this here anywayzzzz</span>
    <span class="n">sigma</span> <span class="o">=</span> <span class="n">sigma_</span><span class="p">;</span> 
    <span class="n">kernel</span><span class="p">();</span>    
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">getwinparams</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">wi</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">he</span><span class="p">)</span>
<span class="p">{</span>
    <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="n">Ox</span><span class="p">;</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">=</span> <span class="n">Oy</span><span class="p">;</span>
    <span class="o">*</span><span class="n">wi</span> <span class="o">=</span> <span class="n">width</span><span class="p">;</span>
    <span class="o">*</span><span class="n">he</span> <span class="o">=</span> <span class="n">height</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">checkbounds</span><span class="p">()</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">offset</span><span class="p">;</span>

	<span class="k">do</span> <span class="p">{</span>
		<span class="n">offset</span> <span class="o">=</span> <span class="n">Ox</span><span class="o">*</span><span class="n">BPP</span> <span class="o">+</span> <span class="p">((</span><span class="n">Oy</span><span class="o">+</span><span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
		<span class="n">y</span><span class="o">++</span><span class="p">;</span>
	<span class="p">}</span><span class="k">while</span><span class="p">((</span><span class="n">offset</span><span class="o">&lt;=</span><span class="p">(</span><span class="n">screensize</span><span class="o">-</span><span class="p">(</span><span class="n">width4</span><span class="p">)))</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">y</span><span class="o">&lt;=</span><span class="n">height</span><span class="p">));</span>
	<span class="n">y</span><span class="o">--</span><span class="p">;</span>
	<span class="c1">//printf(&quot;y = %i \n&quot;,y);</span>
	<span class="k">return</span> <span class="p">(</span><span class="n">y</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">clearbuffer</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">buffy</span><span class="p">)</span>
<span class="p">{</span>
    <span class="cm">/*</span>
<span class="cm">     * utility function</span>
<span class="cm">     * either clear buffer</span>
<span class="cm">     * or framebuffer</span>
<span class="cm">     */</span>

	<span class="kt">int</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">pix_offset</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">maxheight</span><span class="p">;</span>
	<span class="n">maxheight</span> <span class="o">=</span> <span class="n">checkbounds</span><span class="p">();</span>

	<span class="k">for</span> <span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="n">maxheight</span><span class="p">;</span> <span class="n">y</span><span class="o">++</span><span class="p">){</span>
        <span class="n">pix_offset</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="o">+</span><span class="n">Ox</span><span class="o">*</span><span class="n">BPP</span><span class="p">)</span> <span class="o">+</span> <span class="p">((</span><span class="n">Oy</span><span class="o">+</span><span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
        <span class="n">memset</span><span class="p">(</span><span class="n">buffy</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="c1">//update();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">cleartmpbuffer</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">clearbuffer</span><span class="p">(</span><span class="n">tmp_buf</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">clearscreen</span><span class="p">()</span> 
<span class="p">{</span>
    <span class="n">clearbuffer</span><span class="p">(</span><span class="n">fbp</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">closefb</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">//memcpy(fbp, dummy_buf, screensize);</span>
    <span class="n">munmap</span><span class="p">(</span><span class="n">fbp</span><span class="p">,</span> <span class="n">screensize</span><span class="p">);</span>
    <span class="c1">//free(fbp);</span>
    <span class="n">fbp</span><span class="o">=</span><span class="nb">NULL</span><span class="p">;</span>
    <span class="n">free</span><span class="p">(</span><span class="n">currentcolor</span><span class="p">);</span>
    <span class="n">currentcolor</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">free</span><span class="p">(</span><span class="n">currenttrafo</span><span class="p">);</span>
    <span class="n">currenttrafo</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">free</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="p">);</span>
    <span class="n">currenttrafo3</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> 
    <span class="n">free</span><span class="p">(</span><span class="n">tmp_buf</span><span class="p">);</span>
    <span class="n">tmp_buf</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="c1">//if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &amp;orig_vinfo)) {</span>
    <span class="c1">//    fprintf(stderr, &quot;Error re-setting variable information.\n&quot;);</span>
    <span class="c1">//}</span>
    <span class="n">free</span><span class="p">(</span><span class="n">dummy_buf</span><span class="p">);</span>
    <span class="n">dummy_buf</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">close</span><span class="p">(</span><span class="n">fbfd</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">kernel</span><span class="p">()</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">r2</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">MAXBLUR</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">MAXBLUR</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">){</span>
            <span class="n">r2</span> <span class="o">=</span> <span class="p">((</span><span class="n">i</span><span class="o">-</span><span class="n">MAXBLURHALF</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">i</span><span class="o">-</span><span class="n">MAXBLURHALF</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">j</span><span class="o">-</span><span class="n">MAXBLURHALF</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">j</span><span class="o">-</span><span class="n">MAXBLURHALF</span><span class="p">)</span><span class="o">+</span><span class="mf">0.02</span><span class="p">);</span>
            <span class="n">kern</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)(</span><span class="mf">255.0</span><span class="o">*</span><span class="n">expf</span><span class="p">(</span><span class="o">-</span><span class="n">r2</span><span class="o">/</span><span class="n">blurrad</span><span class="o">/</span><span class="n">sigma</span><span class="p">));</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">plotalpha_</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">pix_offset</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">opacity</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">faintb</span><span class="p">,</span> <span class="n">faintg</span><span class="p">,</span> <span class="n">faintr</span><span class="p">,</span> <span class="n">fainta</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">r</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">;</span>

    <span class="c1">//x = Ox + (x % width);</span>
    <span class="c1">//y = Oy + (y % height);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">&gt;</span><span class="n">width</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">y</span><span class="o">&gt;</span><span class="n">height</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">y</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="n">x</span><span class="o">+=</span><span class="n">Ox</span><span class="p">;</span>
    <span class="n">y</span><span class="o">+=</span><span class="n">Oy</span><span class="p">;</span>

    <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">BPP</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">*</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
   
    <span class="k">if</span> <span class="p">(</span><span class="n">pix_offset</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">pix_offset</span><span class="o">&gt;</span><span class="n">screensize</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="n">opacity</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)</span><span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span><span class="p">)</span><span class="o">/</span><span class="mf">255.0</span><span class="p">);</span>

    <span class="n">b</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)(</span><span class="o">*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="p">)));</span>
    <span class="n">g</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)(</span><span class="o">*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">1</span><span class="p">)));</span>
    <span class="n">r</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)(</span><span class="o">*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">2</span><span class="p">)));</span>

    <span class="n">faintb</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)((</span><span class="n">opacity</span> <span class="o">*</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="p">);</span>
    <span class="n">faintr</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)((</span><span class="n">opacity</span> <span class="o">*</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span><span class="p">)</span> <span class="o">+</span> <span class="n">r</span><span class="p">);</span>
    <span class="n">faintg</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)((</span><span class="n">opacity</span> <span class="o">*</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span><span class="p">)</span> <span class="o">+</span> <span class="n">g</span><span class="p">);</span>

    <span class="n">fainta</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)(</span><span class="mf">255.0</span> <span class="o">-</span> <span class="p">((</span><span class="mi">255</span><span class="o">-*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)))</span> <span class="o">+</span> <span class="p">(</span><span class="mi">255</span><span class="o">-</span><span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span><span class="p">)));</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">fainta</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="n">fainta</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="c1">// what we want </span>
    <span class="c1">// 0 = opacity., 255 = opaq</span>
    <span class="c1">// old + new</span>
    <span class="c1">// but we have</span>
    <span class="c1">// 0 = opaq, 255 = opacity</span>
    <span class="c1">// 255 - ((255-old) + (255-new))</span>
    <span class="c1">// 255 - (510 -old -new) = -250 + old + new</span>
    <span class="c1">//</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">faintb</span><span class="o">&gt;</span><span class="mi">255</span><span class="p">)</span> <span class="n">faintb</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="mi">255</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">faintr</span><span class="o">&gt;</span><span class="mi">255</span><span class="p">)</span> <span class="n">faintr</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="mi">255</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">faintg</span><span class="o">&gt;</span><span class="mi">255</span><span class="p">)</span> <span class="n">faintg</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="mi">255</span><span class="p">;</span>

    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="p">))</span>  <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)</span><span class="n">faintb</span><span class="p">;</span>
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span> <span class="o">=</span>  <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)</span><span class="n">faintg</span><span class="p">;</span>
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">2</span><span class="p">))</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)</span><span class="n">faintr</span><span class="p">;</span>
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">3</span><span class="p">))</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)</span><span class="n">fainta</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">plotblurred_</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">struct</span> <span class="n">Color</span> <span class="n">tmpcol</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">fucktor</span><span class="p">;</span>           <span class="c1">//r squared, yih</span>
    <span class="kt">int</span> <span class="n">r_max</span> <span class="o">=</span> <span class="n">blurrad</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">startx</span><span class="p">,</span> <span class="n">starty</span><span class="p">,</span> <span class="n">endx</span><span class="p">,</span> <span class="n">endy</span><span class="p">;</span>

    <span class="n">tmpcol</span><span class="p">.</span><span class="n">r</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span><span class="p">;</span>
    <span class="n">tmpcol</span><span class="p">.</span><span class="n">g</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span><span class="p">;</span>
    <span class="n">tmpcol</span><span class="p">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span><span class="p">;</span>
    <span class="n">tmpcol</span><span class="p">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span><span class="p">;</span>

    <span class="n">startx</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">r_max</span><span class="p">)</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="mi">0</span><span class="o">:</span><span class="n">x</span><span class="o">-</span><span class="n">r_max</span><span class="p">;</span>
    <span class="n">endx</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">r_max</span><span class="p">)</span><span class="o">&gt;</span><span class="p">(</span><span class="n">Ox</span><span class="o">+</span><span class="n">width</span><span class="p">)</span><span class="o">?</span><span class="p">(</span><span class="n">Ox</span><span class="o">+</span><span class="n">width</span><span class="p">)</span><span class="o">:</span><span class="n">x</span><span class="o">+</span><span class="n">r_max</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">starty</span> <span class="o">=</span> <span class="p">(</span><span class="n">y</span><span class="o">-</span><span class="n">r_max</span><span class="p">)</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="mi">0</span><span class="o">:</span><span class="n">y</span><span class="o">-</span><span class="n">r_max</span><span class="p">;</span>
    <span class="n">endy</span> <span class="o">=</span> <span class="p">(</span><span class="n">y</span><span class="o">+</span><span class="n">r_max</span><span class="p">)</span><span class="o">&gt;</span><span class="p">(</span><span class="n">Oy</span><span class="o">+</span><span class="n">height</span><span class="p">)</span><span class="o">?</span><span class="p">(</span><span class="n">Oy</span><span class="o">+</span><span class="n">height</span><span class="p">)</span><span class="o">:</span><span class="n">y</span><span class="o">+</span><span class="n">r_max</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>

    <span class="n">fucktor</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span><span class="o">-</span><span class="p">(</span><span class="n">tmpcol</span><span class="p">.</span><span class="n">a</span><span class="o">/</span><span class="mf">255.0</span><span class="p">));</span> 

    <span class="c1">//kernel();</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="n">startx</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">endx</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span><span class="o">=</span><span class="n">starty</span><span class="p">;</span> <span class="n">j</span><span class="o">&lt;</span><span class="n">endy</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">){</span>
            <span class="c1">// if the exp =0 a should be 255</span>
            <span class="c1">// if exp =1 a below 255 but not 0</span>
            <span class="c1">//</span>
            <span class="c1">// 1 - (1-a)gauss</span>
            <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span><span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)(</span><span class="mf">255.0</span> <span class="o">-</span> <span class="n">fucktor</span> <span class="o">*</span> <span class="n">kern</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="n">x</span><span class="o">+</span><span class="n">MAXBLURHALF</span><span class="p">][</span><span class="n">j</span><span class="o">-</span><span class="n">y</span><span class="o">+</span><span class="n">MAXBLURHALF</span><span class="p">]);</span>
            <span class="n">plotalpha_</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">a</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">r</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">g</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">b</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">get_pixel</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="k">struct</span> <span class="n">Color</span> <span class="o">*</span><span class="n">color</span><span class="p">,</span>  <span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">pix_offset</span><span class="p">;</span>

    <span class="n">x</span> <span class="o">=</span> <span class="n">Ox</span> <span class="o">+</span> <span class="p">(</span><span class="n">x</span> <span class="o">%</span> <span class="n">width</span><span class="p">);</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">Oy</span> <span class="o">+</span> <span class="p">(</span><span class="n">y</span> <span class="o">%</span> <span class="n">height</span><span class="p">);</span>

    <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">BPP</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">*</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>

    <span class="n">color</span><span class="o">-&gt;</span><span class="n">b</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">buffer</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="p">));</span>
    <span class="n">color</span><span class="o">-&gt;</span><span class="n">g</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">buffer</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">1</span><span class="p">));</span>
    <span class="n">color</span><span class="o">-&gt;</span><span class="n">r</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">buffer</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">2</span><span class="p">));</span>
    <span class="n">color</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">buffer</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">3</span><span class="p">));</span>
        
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">plot_</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">//this is gonna be the fast implementation.</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">pix_offset</span><span class="p">;</span>

    <span class="n">x</span> <span class="o">=</span> <span class="n">Ox</span> <span class="o">+</span> <span class="p">(</span><span class="n">x</span> <span class="o">%</span> <span class="n">width</span><span class="p">);</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">Oy</span> <span class="o">+</span> <span class="p">(</span><span class="n">y</span> <span class="o">%</span> <span class="n">height</span><span class="p">);</span>

    <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">BPP</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">*</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
    
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="p">))</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span><span class="p">;</span>
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span><span class="p">;</span>
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">2</span><span class="p">))</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span><span class="p">;</span>
    <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="n">pix_offset</span><span class="o">+</span><span class="mi">3</span><span class="p">))</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">plot</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">pPlot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">fillrect</span><span class="p">(</span><span class="kt">int</span> <span class="n">x0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">w</span><span class="p">,</span> <span class="kt">int</span> <span class="n">h</span><span class="p">)</span> 
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">y</span><span class="p">;</span>

	<span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">x</span><span class="o">&lt;</span><span class="n">w</span><span class="p">;</span><span class="n">x</span><span class="o">++</span><span class="p">)</span>
		<span class="k">for</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">y</span><span class="o">&lt;</span><span class="n">h</span><span class="p">;</span><span class="n">y</span><span class="o">++</span><span class="p">)</span>
			<span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">x</span><span class="p">,</span> <span class="n">y0</span><span class="o">+</span><span class="n">y</span><span class="p">);</span>

	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">snow</span><span class="p">()</span> 
<span class="p">{</span>
	<span class="c1">//http://stackoverflow.com/questions/2572366/how-to-use-dev-random-or-urandom-in-c</span>
	<span class="c1">//int surfsize =0;</span>
	<span class="c1">//int pix_offset;</span>
	<span class="c1">//int sub_offset;	</span>
	<span class="c1">//int y;</span>
	<span class="c1">//char *snow_buf;</span>
	<span class="c1">//FILE *fp;</span>

    <span class="n">tvsnow</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
    <span class="cm">/*</span>
<span class="cm">	surfsize = height * width4;</span>

<span class="cm">	snow_buf = malloc((size_t)surfsize);</span>

<span class="cm">	fp = fopen(&quot;/dev/urandom&quot;, &quot;r&quot;);</span>
<span class="cm">	fread(snow_buf, 1, surfsize, fp);</span>
<span class="cm">	fclose(fp);</span>
<span class="cm">	for (y=0; y&lt;height;y++){</span>
<span class="cm">		pix_offset = (0+Ox*4) + ((Oy+y)*finfo.line_length);</span>
<span class="cm">		sub_offset = y*width4;</span>
<span class="cm">		memcpy(tmp_buf+pix_offset, snow_buf+sub_offset, width4);</span>
<span class="cm">	}</span>
<span class="cm">	free(snow_buf);</span>
<span class="cm">	snow_buf=NULL;</span>
<span class="cm">	return 0;</span>
<span class="cm">    */</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">tvsnow</span><span class="p">()</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">surfsize</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span>
    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">snow_buf</span><span class="o">=</span><span class="nb">NULL</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">Color</span> <span class="n">tmpcol</span><span class="p">;</span>
    <span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span><span class="p">;</span>
    <span class="n">surfsize</span><span class="o">=</span><span class="n">height</span><span class="o">*</span><span class="n">width4</span><span class="p">;</span>
    <span class="n">snow_buf</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">((</span><span class="kt">size_t</span><span class="p">)</span><span class="n">surfsize</span><span class="p">);</span>

    <span class="n">fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="s">&quot;/dev/urandom&quot;</span><span class="p">,</span> <span class="s">&quot;r&quot;</span><span class="p">);</span>
	<span class="n">fread</span><span class="p">(</span><span class="n">snow_buf</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">surfsize</span><span class="p">,</span> <span class="n">fp</span><span class="p">);</span>
	<span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>

    <span class="n">tmpcol</span><span class="p">.</span><span class="n">r</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span><span class="p">;</span>
    <span class="n">tmpcol</span><span class="p">.</span><span class="n">g</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span><span class="p">;</span>
    <span class="n">tmpcol</span><span class="p">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span><span class="p">;</span>
    <span class="n">tmpcol</span><span class="p">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span> <span class="n">x</span><span class="o">&lt;</span><span class="p">(</span><span class="n">width</span><span class="p">);</span><span class="n">x</span><span class="o">+=</span><span class="mi">5</span><span class="p">){</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span><span class="n">y</span><span class="o">&lt;</span> <span class="p">(</span><span class="n">height</span><span class="p">);</span><span class="n">y</span><span class="o">+=</span><span class="mi">5</span><span class="p">){</span>
            <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">snow_buf</span><span class="o">+</span><span class="n">BPP</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="p">(</span><span class="n">y</span><span class="o">*</span><span class="n">width4</span><span class="p">)));</span>
            <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">snow_buf</span><span class="o">+</span><span class="n">BPP</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="p">(</span><span class="n">y</span><span class="o">*</span><span class="n">width4</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">));</span>
            <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)(</span><span class="n">snow_buf</span><span class="o">+</span><span class="n">BPP</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="p">(</span><span class="n">y</span><span class="o">*</span><span class="n">width4</span><span class="p">)</span><span class="o">+</span><span class="mi">2</span><span class="p">));</span>
            <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="p">)</span><span class="mi">0</span><span class="p">;</span><span class="c1">//*((char*)(snow_buf+BPP*x+(y*width4)));</span>
            
            <span class="n">plotblurred_</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">Ox</span><span class="p">,</span> <span class="n">y</span><span class="o">+</span><span class="n">Oy</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="n">free</span><span class="p">(</span><span class="n">snow_buf</span><span class="p">);</span>
    <span class="n">snow_buf</span><span class="o">=</span><span class="nb">NULL</span><span class="p">;</span>

    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">a</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">r</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">g</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span> <span class="o">=</span> <span class="n">tmpcol</span><span class="p">.</span><span class="n">b</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">waitforvsync</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>

<span class="cm">/* fb_vsync:</span>
<span class="cm"> *  Waits for a retrace.</span>
<span class="cm"> *  straight from fbcon.c //cellom</span>
<span class="cm"> */</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">prev</span><span class="p">;</span>

<span class="cp">    #ifdef FBIOGET_VBLANK</span>

    <span class="k">struct</span> <span class="n">fb_vblank</span> <span class="n">vblank</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">vblank_flags</span> <span class="o">&amp;</span> <span class="n">FB_VBLANK_HAVE_STICKY</span><span class="p">)</span> <span class="p">{</span>
       <span class="cm">/* it&#39;s really good if sticky bits are available */</span>
       <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VBLANK</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vblank</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
      <span class="k">return</span> <span class="o">-</span><span class="mi">3</span><span class="p">;</span>

       <span class="k">do</span> <span class="p">{</span> 
		<span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VBLANK</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vblank</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
			<span class="k">break</span><span class="p">;</span>
       <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">vblank</span><span class="p">.</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">FB_VBLANK_STICKY</span><span class="p">));</span>
   <span class="p">}</span>
   <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">vblank_flags</span> <span class="o">&amp;</span> <span class="n">FB_VBLANK_HAVE_VCOUNT</span><span class="p">)</span> <span class="p">{</span>
      <span class="cm">/* we can read the exact scanline position, which avoids skipping */</span>
      <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VBLANK</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vblank</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
     <span class="k">return</span> <span class="o">-</span><span class="mi">3</span><span class="p">;</span>

      <span class="k">do</span> <span class="p">{</span>
     <span class="n">prev</span> <span class="o">=</span> <span class="n">vblank</span><span class="p">.</span><span class="n">vcount</span><span class="p">;</span>
     <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VBLANK</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vblank</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">break</span><span class="p">;</span>
      <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">vblank</span><span class="p">.</span><span class="n">vcount</span> <span class="o">&gt;=</span> <span class="n">prev</span><span class="p">);</span>
   <span class="p">}</span>
   <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">vblank_flags</span> <span class="o">&amp;</span> <span class="n">FB_VBLANK_HAVE_VBLANK</span><span class="p">)</span> <span class="p">{</span>
      <span class="cm">/* boring, normal style poll operation */</span>
      <span class="k">do</span> <span class="p">{</span>
     <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VBLANK</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vblank</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">break</span><span class="p">;</span>
      <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">vblank</span><span class="p">.</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">FB_VBLANK_VBLANKING</span><span class="p">);</span>

      <span class="k">do</span> <span class="p">{</span>
     <span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">fbfd</span><span class="p">,</span> <span class="n">FBIOGET_VBLANK</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">vblank</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">break</span><span class="p">;</span>
      <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">vblank</span><span class="p">.</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">FB_VBLANK_VBLANKING</span><span class="p">));</span>
   <span class="p">}</span>
   <span class="k">else</span> <span class="p">{}</span><span class="c1">//no need for swearing printf(&quot;No SUPPORT for vsync shit, you FUCK\nAARHGGGHH!!!!&quot;);</span>

<span class="cp"> #endif</span>

   <span class="cm">/* bodged implementation for when the framebuffer doesn&#39;t support it */</span>
   <span class="cm">/* We must not to run this loop while returning from a VT switch as timer</span>
<span class="cm">    * &quot;interrupts&quot; will not be running at that time so retrace_count will</span>
<span class="cm">    * remain constant.</span>
<span class="cm">    </span>
<span class="cm">   if (_timer_installed &amp;&amp; !in_fb_restore) {</span>
<span class="cm">      prev = retrace_count;</span>

<span class="cm">      do {</span>
<span class="cm">      } while (retrace_count == (int)prev);</span>
<span class="cm">   }*/</span>
      <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>    

<span class="kt">int</span> <span class="nf">styledredraw</span><span class="p">()</span>
<span class="p">{</span>
    <span class="cm">/*</span>
<span class="cm">     * this function redraws the canvas using </span>
<span class="cm">     * the plot function, thus applying</span>
<span class="cm">     * pixelstyle</span>
<span class="cm">     */</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">xr</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">yr</span><span class="p">;</span>

    <span class="k">struct</span> <span class="n">Color</span> <span class="n">tempcolor</span><span class="p">;</span>

    <span class="c1">//store buffer in temp mem  </span>
    <span class="n">memcpy</span><span class="p">(</span><span class="n">dummy_buf</span><span class="p">,</span> <span class="n">tmp_buf</span><span class="p">,</span> <span class="n">screensize</span><span class="p">);</span> 
  
    <span class="n">tempcolor</span><span class="p">.</span><span class="n">r</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span><span class="p">;</span>
    <span class="n">tempcolor</span><span class="p">.</span><span class="n">g</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span><span class="p">;</span>
    <span class="n">tempcolor</span><span class="p">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span><span class="p">;</span>
    <span class="n">tempcolor</span><span class="p">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span><span class="p">;</span>
  
    <span class="c1">//redraw buffer form temp mem</span>
    <span class="c1">//first clear it</span>

    <span class="n">clearbuffer</span><span class="p">(</span><span class="n">tmp_buf</span><span class="p">);</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">x</span><span class="o">&lt;</span><span class="n">width</span><span class="p">;</span><span class="n">x</span><span class="o">++</span><span class="p">){</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">y</span><span class="o">&lt;</span><span class="n">height</span><span class="p">;</span><span class="n">y</span><span class="o">++</span><span class="p">){</span>
            <span class="n">get_pixel</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">currentcolor</span><span class="p">,</span> <span class="n">dummy_buf</span><span class="p">);</span> 
            <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="n">tempcolor</span><span class="p">.</span><span class="n">a</span><span class="p">;</span> 
            <span class="n">xr</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>
            <span class="n">yr</span> <span class="o">=</span> <span class="n">y</span><span class="p">;</span>  
            <span class="k">if</span> <span class="p">((</span><span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span><span class="o">!=</span><span class="mi">0</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span><span class="o">!=</span><span class="mi">0</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span><span class="o">!=</span><span class="mi">0</span><span class="p">)){</span>
            <span class="c1">//aint gonna tranform nonvisible pixels pixels. cmon.</span>
                <span class="n">transform</span><span class="p">(</span><span class="o">&amp;</span><span class="n">xr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">yr</span><span class="p">);</span>
                <span class="k">if</span> <span class="p">((</span><span class="n">xr</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="n">xr</span><span class="o">&lt;</span><span class="n">width</span><span class="p">)){</span>
                    <span class="k">if</span> <span class="p">((</span><span class="n">yr</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">)</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">yr</span><span class="o">&lt;</span><span class="n">height</span><span class="p">)){</span>
                        <span class="n">pPlot</span><span class="p">(</span><span class="n">xr</span><span class="p">,</span><span class="n">yr</span><span class="p">);</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span> 
    
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">a</span> <span class="o">=</span> <span class="n">tempcolor</span><span class="p">.</span><span class="n">a</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">r</span> <span class="o">=</span> <span class="n">tempcolor</span><span class="p">.</span><span class="n">r</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">g</span> <span class="o">=</span> <span class="n">tempcolor</span><span class="p">.</span><span class="n">g</span><span class="p">;</span>
    <span class="n">currentcolor</span><span class="o">-&gt;</span><span class="n">b</span> <span class="o">=</span> <span class="n">tempcolor</span><span class="p">.</span><span class="n">b</span><span class="p">;</span>
    
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">update</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">pix_offset</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">maxheight</span><span class="p">;</span>
    <span class="n">maxheight</span> <span class="o">=</span> <span class="n">checkbounds</span><span class="p">();</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="n">maxheight</span><span class="p">;</span> <span class="n">y</span><span class="o">++</span><span class="p">){</span>
        <span class="n">pix_offset</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="o">+</span><span class="n">Ox</span><span class="o">*</span><span class="n">BPP</span><span class="p">)</span> <span class="o">+</span> <span class="p">((</span><span class="n">Oy</span><span class="o">+</span><span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
	    <span class="n">waitforvsync</span><span class="p">();</span>
        <span class="n">memcpy</span><span class="p">(</span><span class="n">fbp</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">width4</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">poly</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">)</span> 
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="n">l</span><span class="o">-=</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">l</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">){</span>
	    <span class="n">line</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">matrixvec3</span><span class="p">(</span><span class="kt">double</span> <span class="n">m</span><span class="p">[</span><span class="mi">9</span><span class="p">],</span> <span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">double</span> <span class="n">xl</span><span class="p">,</span><span class="n">yl</span><span class="p">,</span><span class="n">zl</span><span class="p">;</span>

    <span class="n">xl</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">m</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">m</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">z</span><span class="p">);</span>
    <span class="n">yl</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)(</span><span class="n">m</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">m</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">m</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">z</span><span class="p">);</span>
    <span class="n">zl</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)(</span><span class="n">m</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">m</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">m</span><span class="p">[</span><span class="mi">8</span><span class="p">]</span><span class="o">*</span> <span class="o">*</span><span class="n">z</span><span class="p">);</span>
    
    <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">xl</span><span class="p">;</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">yl</span><span class="p">;</span>
    <span class="o">*</span><span class="n">z</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">zl</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">rotate</span><span class="p">(</span><span class="kt">double</span> <span class="n">angle</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="kt">char</span> <span class="n">axis</span><span class="p">)</span>
<span class="p">{</span>
    <span class="cm">/*</span>
<span class="cm">     * positioning values in rotation matrix</span>
<span class="cm">     * value    Z   Y   X</span>
<span class="cm">     * ------------------</span>
<span class="cm">     * cos      0   0   4</span>
<span class="cm">     * cos      4   8   8</span>
<span class="cm">     * sin      3   2   7</span>
<span class="cm">     * -sin     1   6   5   </span>
<span class="cm">     * 1        8   4   0</span>
<span class="cm">     * 0        2   1   1</span>
<span class="cm">     * 0        5   3   2</span>
<span class="cm">     * 0        6   5   3</span>
<span class="cm">     * 0        7   7   6</span>
<span class="cm">     */</span> 

    <span class="kt">double</span> <span class="n">m</span><span class="p">[</span><span class="mi">9</span><span class="p">];</span>
    <span class="kt">double</span> <span class="n">C</span><span class="p">,</span><span class="n">S</span><span class="p">,</span><span class="n">nS</span><span class="p">;</span>
    <span class="k">static</span> <span class="n">R</span><span class="p">[</span><span class="mi">27</span><span class="p">]</span><span class="o">=</span><span class="p">{</span><span class="mi">4</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span>
                  <span class="mi">0</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span>
                  <span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">};</span>
    <span class="n">C</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="o">-</span><span class="n">angle</span><span class="p">);</span>
    <span class="n">S</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="o">-</span><span class="n">angle</span><span class="p">);</span>
    <span class="n">nS</span><span class="o">=</span> <span class="o">-</span><span class="n">S</span><span class="p">;</span>

    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">0</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="n">C</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">1</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="n">C</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">2</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="n">S</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">3</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="n">nS</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">4</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">5</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">6</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">7</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>
    <span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">+*</span><span class="p">(</span><span class="n">R</span><span class="o">+</span><span class="mi">8</span><span class="o">+</span><span class="n">axis</span><span class="p">))</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>

    <span class="n">matrixvec3</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">);</span>    

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">project</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">double</span> <span class="n">bx</span><span class="p">,</span><span class="n">by</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span>    
            <span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetaz</span><span class="o">==</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
            <span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetay</span><span class="o">==</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
            <span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetax</span><span class="o">==</span><span class="mf">0.0</span><span class="p">)</span> <span class="p">){</span>
        <span class="k">goto</span> <span class="n">Translate</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">rotate</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetaz</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">zaxis</span><span class="p">);</span>
        <span class="n">rotate</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetay</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">yaxis</span><span class="p">);</span>
        <span class="n">rotate</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">tetax</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">xaxis</span><span class="p">);</span>
    <span class="p">}</span>
<span class="nl">Translate:</span>
    <span class="o">*</span><span class="n">x</span> <span class="o">-=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">cx</span><span class="p">;</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">-=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">cy</span><span class="p">;</span>
    <span class="o">*</span><span class="n">z</span> <span class="o">-=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">cz</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span>
            <span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetaz</span><span class="o">==</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
            <span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetay</span><span class="o">==</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
            <span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetax</span><span class="o">==</span><span class="mf">0.0</span><span class="p">)</span> <span class="p">){</span>
        <span class="k">goto</span> <span class="n">projection</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">rotate</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetaz</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">zaxis</span><span class="p">);</span>
        <span class="n">rotate</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetay</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">yaxis</span><span class="p">);</span>
        <span class="n">rotate</span><span class="p">(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ctetax</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">xaxis</span><span class="p">);</span>
    <span class="p">}</span>
<span class="nl">projection:</span>    
    <span class="k">if</span> <span class="p">((</span><span class="o">*</span><span class="n">z</span><span class="o">&gt;-</span><span class="mi">1</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="c1">//    *z=-1.0;</span>
    <span class="n">bx</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ez</span><span class="o">/</span> <span class="o">*</span><span class="n">z</span> <span class="o">*</span> <span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ex</span><span class="p">);</span>
    <span class="n">by</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)(</span><span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ez</span><span class="o">/</span> <span class="o">*</span><span class="n">z</span> <span class="o">*</span> <span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">currenttrafo3</span><span class="o">-&gt;</span><span class="n">ey</span><span class="p">);</span>

    <span class="c1">//bx = bx&lt;0?0.0:bx;</span>
    <span class="c1">//bx = bx&gt;vinfo.xres?vinfo.xres:bx;</span>
    <span class="c1">//by = by&lt;0?0.0:by;</span>
    <span class="c1">//by = by&gt;vinfo.yres?vinfo.yres:by;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">bx</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">bx</span><span class="o">&gt;</span><span class="n">width</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">by</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">by</span><span class="o">&gt;</span><span class="n">height</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">bx</span><span class="p">;</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">by</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">poly3d</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">)</span>
<span class="p">{</span>
    <span class="cm">/*</span>
<span class="cm">     * TODO:</span>
<span class="cm">     * I am going to pass by ref</span>
<span class="cm">     * and the project decides whether</span>
<span class="cm">     * to mess up or not...</span>
<span class="cm">     * you might wanna have a &#39;state&#39;</span>
<span class="cm">     * gott think this over...</span>
<span class="cm">     */</span>

    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">xi</span><span class="p">,</span><span class="n">yi</span><span class="p">,</span><span class="n">zi</span><span class="p">;</span>   <span class="c1">//local copies for messup</span>
    <span class="kt">int</span> <span class="n">xf</span><span class="p">,</span><span class="n">yf</span><span class="p">,</span><span class="n">zf</span><span class="p">;</span>

    <span class="n">l</span><span class="o">-=</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">l</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="n">xi</span><span class="o">=</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
        <span class="n">yi</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
        <span class="n">zi</span><span class="o">=</span><span class="n">z</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
        <span class="n">xf</span><span class="o">=</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span>
        <span class="n">yf</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span>
        <span class="n">zf</span><span class="o">=</span><span class="n">z</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">project</span><span class="p">(</span><span class="o">&amp;</span><span class="n">xi</span><span class="p">,</span><span class="o">&amp;</span><span class="n">yi</span><span class="p">,</span><span class="o">&amp;</span><span class="n">zi</span><span class="p">))</span>
            <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">project</span><span class="p">(</span><span class="o">&amp;</span><span class="n">xf</span><span class="p">,</span><span class="o">&amp;</span><span class="n">yf</span><span class="p">,</span><span class="o">&amp;</span><span class="n">zf</span><span class="p">))</span>
                <span class="n">line</span><span class="p">(</span><span class="n">xi</span><span class="p">,</span><span class="n">yi</span><span class="p">,</span><span class="n">xf</span><span class="p">,</span><span class="n">yf</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">drawpolys</span><span class="p">(</span><span class="n">Polys</span> <span class="o">*</span><span class="n">p</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">l</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">polyc</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="n">l</span> <span class="o">=</span> <span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">polyl</span><span class="o">+</span><span class="n">i</span><span class="p">);</span>
        <span class="n">poly</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">x</span><span class="o">+</span><span class="n">i</span><span class="p">),</span> <span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">y</span><span class="o">+</span><span class="n">i</span><span class="p">),</span> <span class="n">l</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">draw3dpolys</span><span class="p">(</span><span class="n">Polys</span> <span class="o">*</span><span class="n">p</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">l</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">polyc</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="n">l</span><span class="o">=*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">polyl</span><span class="o">+</span><span class="n">i</span><span class="p">);</span>
        <span class="n">poly3d</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">x</span><span class="o">+</span><span class="n">i</span><span class="p">),</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">y</span><span class="o">+</span><span class="n">i</span><span class="p">),</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">z</span><span class="o">+</span><span class="n">i</span><span class="p">),</span><span class="n">l</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">rotateX</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">rotate</span><span class="p">(</span><span class="n">trpms</span><span class="o">-&gt;</span><span class="n">tetax</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">xaxis</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">rotateY</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">rotate</span><span class="p">(</span><span class="n">trpms</span><span class="o">-&gt;</span><span class="n">tetay</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">yaxis</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">rotateZ</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">rotate</span><span class="p">(</span><span class="n">trpms</span><span class="o">-&gt;</span><span class="n">tetaz</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span><span class="n">zaxis</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">identity</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">translate</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">)</span>
<span class="p">{</span>
    <span class="o">*</span><span class="n">x</span> <span class="o">+=</span> <span class="n">trpms</span><span class="o">-&gt;</span><span class="n">dx</span><span class="p">;</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">+=</span> <span class="n">trpms</span><span class="o">-&gt;</span><span class="n">dy</span><span class="p">;</span>
    <span class="o">*</span><span class="n">z</span> <span class="o">+=</span> <span class="n">trpms</span><span class="o">-&gt;</span><span class="n">dz</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">seltrafo</span><span class="p">(</span>   <span class="n">Polys</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">,</span> 
                <span class="kt">int</span> <span class="p">(</span><span class="o">*</span><span class="n">lintrafo</span><span class="p">[])</span> <span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="p">,</span> <span class="kt">int</span><span class="o">*</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="p">),</span>
                <span class="kt">int</span> <span class="o">*</span><span class="n">order</span><span class="p">,</span> <span class="kt">int</span> <span class="n">numtrafo</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">l</span><span class="p">;</span>
    <span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">;</span>
    <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">;</span>
    <span class="kt">int</span> <span class="o">*</span><span class="n">z</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">polyc</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="n">l</span><span class="o">=*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">polyl</span><span class="o">+</span><span class="n">i</span><span class="p">);</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">l</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">){</span>
            <span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">x</span><span class="o">+</span><span class="n">i</span><span class="p">)</span><span class="o">+</span><span class="n">j</span><span class="p">);</span>
            <span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">y</span><span class="o">+</span><span class="n">i</span><span class="p">)</span><span class="o">+</span><span class="n">j</span><span class="p">);</span>
            <span class="n">z</span> <span class="o">=</span> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">z</span><span class="o">+</span><span class="n">i</span><span class="p">)</span><span class="o">+</span><span class="n">j</span><span class="p">);</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">k</span><span class="o">&lt;</span><span class="n">numtrafo</span><span class="p">;</span><span class="n">k</span><span class="o">++</span><span class="p">)</span>
                <span class="n">lintrafo</span><span class="p">[</span><span class="o">*</span><span class="p">(</span><span class="n">order</span><span class="o">+</span><span class="n">k</span><span class="p">)](</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">trpms</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">transform3d</span><span class="p">(</span><span class="n">Polys</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="k">struct</span> <span class="n">TrafoL</span> <span class="o">*</span><span class="n">trpms</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">order</span><span class="p">,</span><span class="kt">int</span> <span class="n">numtrafo</span><span class="p">)</span>
<span class="p">{</span>   
    <span class="n">lintrafo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">identity</span><span class="p">;</span>
    <span class="n">lintrafo</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">rotateX</span><span class="p">;</span>
    <span class="n">lintrafo</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">rotateY</span><span class="p">;</span>
    <span class="n">lintrafo</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">rotateZ</span><span class="p">;</span>
    <span class="n">lintrafo</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">translate</span><span class="p">;</span>
    <span class="n">seltrafo</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">trpms</span><span class="p">,</span> <span class="n">lintrafo</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">numtrafo</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">memblockcpy</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">to_buffer</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">char</span> <span class="n">DIRECTION</span><span class="p">)</span>
<span class="p">{</span> 
	<span class="c1">//this is a helper function</span>
	<span class="c1">//will probably put in separate</span>
	<span class="c1">//lib function, with optimized</span>
	<span class="c1">//compiler settings?</span>
	<span class="c1">//NOT YET FINNISHED!!!!!!!!!!</span>
	<span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">pix_offset</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">sub_offset</span><span class="p">;</span>
	<span class="c1">//sub_offset = y * width * BPP</span>
	<span class="c1">//and cannot exceed l</span>
	<span class="c1">//l = y * width * BPP =&gt; y_max = l / (width * BPP)</span>
	<span class="c1">//because of tmp_buff on the other hand, y_max cannot</span>
	<span class="c1">//exceed height</span>
	
	<span class="n">y</span> <span class="o">=</span> <span class="n">l</span> <span class="o">/</span> <span class="n">width4</span><span class="p">;</span>
	<span class="k">if</span> <span class="p">(</span><span class="n">y</span><span class="o">&gt;</span><span class="n">height</span><span class="p">)</span> <span class="n">y</span><span class="o">=</span><span class="n">height</span><span class="p">;</span>

	<span class="c1">//guess this covers it for the moment.</span>

	<span class="k">if</span> <span class="p">(</span><span class="n">DIRECTION</span> <span class="o">==</span> <span class="n">TO_BLOCK</span><span class="p">){</span>
		<span class="k">while</span><span class="p">(</span><span class="n">y</span><span class="o">--</span><span class="p">){</span>
			<span class="n">pix_offset</span> <span class="o">=</span> <span class="p">(</span><span class="n">Ox</span><span class="o">*</span><span class="n">BPP</span><span class="p">)</span> <span class="o">+</span> <span class="p">((</span><span class="n">Oy</span><span class="o">+</span><span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
			<span class="n">sub_offset</span> <span class="o">=</span> <span class="n">y</span><span class="o">*</span><span class="n">width4</span><span class="p">;</span>
			<span class="n">memcpy</span><span class="p">(</span><span class="n">to_buffer</span><span class="o">+</span><span class="n">sub_offset</span><span class="p">,</span> <span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">width4</span><span class="p">);</span>
		<span class="p">}</span>	
	<span class="p">}</span>

	<span class="k">if</span> <span class="p">(</span><span class="n">DIRECTION</span> <span class="o">==</span> <span class="n">FROM_BLOCK</span><span class="p">){</span>
		<span class="k">while</span><span class="p">(</span><span class="n">y</span><span class="o">--</span><span class="p">){</span>
			<span class="n">pix_offset</span> <span class="o">=</span> <span class="p">(</span><span class="n">Ox</span><span class="o">*</span><span class="n">BPP</span><span class="p">)</span> <span class="o">+</span> <span class="p">((</span><span class="n">Oy</span><span class="o">+</span><span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
			<span class="n">sub_offset</span> <span class="o">=</span> <span class="n">y</span><span class="o">*</span><span class="n">width4</span><span class="p">;</span>
			<span class="n">memcpy</span><span class="p">(</span><span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">to_buffer</span><span class="o">+</span><span class="n">sub_offset</span><span class="p">,</span> <span class="n">width4</span><span class="p">);</span>
		<span class="p">}</span>	
	<span class="p">}</span>
	
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>

<span class="p">}</span>

<span class="kt">int</span> <span class="nf">get_raw</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">sprite_buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">memblockcpy</span><span class="p">(</span><span class="n">sprite_buf</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">TO_BLOCK</span><span class="p">);</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">set_raw</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">sprite_buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">l</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">memblockcpy</span><span class="p">(</span><span class="n">sprite_buf</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">FROM_BLOCK</span><span class="p">);</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">transform</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">//here we do some linalg. it is soooo</span>
    <span class="c1">//simple: no gsl or thelike used</span>
    <span class="c1">//TODO:</span>
    <span class="c1">//apply petit 1998 stuff</span>

    <span class="kt">float</span> <span class="n">tmpx</span><span class="p">,</span> <span class="n">tmpy</span><span class="p">,</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">centerX</span><span class="p">,</span> <span class="n">centerY</span><span class="p">;</span>

    <span class="c1">//come on, wont go through the trouble</span>
    <span class="c1">//if it is a unity transform really</span>
    <span class="c1">//mayB some otha trick in the future</span>
    <span class="c1">//TODO:</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">unity</span><span class="p">)</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>

    <span class="c1">//for the moment we assume rotation around</span>
    <span class="c1">//the center of the surface.</span>
    <span class="n">centerX</span> <span class="o">=</span> <span class="n">width</span><span class="o">/</span><span class="mf">2.0</span><span class="p">;</span>
    <span class="n">centerY</span> <span class="o">=</span> <span class="n">height</span><span class="o">/</span><span class="mf">2.0</span><span class="p">;</span>

    <span class="n">X</span> <span class="o">=</span> <span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="n">centerX</span><span class="p">;</span>
    <span class="n">Y</span> <span class="o">=</span> <span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">centerY</span><span class="p">;</span>

    <span class="n">tmpx</span> <span class="o">=</span> <span class="p">(</span><span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m11</span> <span class="o">*</span> <span class="n">X</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m12</span> <span class="o">*</span> <span class="n">Y</span><span class="p">);</span>
    <span class="n">tmpy</span> <span class="o">=</span> <span class="p">(</span><span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m21</span> <span class="o">*</span> <span class="n">X</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">currenttrafo</span><span class="o">-&gt;</span><span class="n">m22</span> <span class="o">*</span> <span class="n">Y</span><span class="p">);</span>

    <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">tmpx</span> <span class="o">+</span> <span class="n">centerX</span><span class="p">);</span>
    <span class="o">*</span><span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">tmpy</span> <span class="o">+</span> <span class="n">centerY</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">line</span><span class="p">(</span><span class="kt">int</span> <span class="n">x0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">x1</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y1</span><span class="p">)</span> 
<span class="p">{</span>
    <span class="c1">//http://rosettacode.org/wiki/Bitmap/Bresenham&#39;s_line_algorithm</span>

    <span class="kt">int</span> <span class="n">dx</span><span class="p">,</span> <span class="n">dy</span><span class="p">,</span> <span class="n">err</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">sx</span><span class="p">,</span> <span class="n">sy</span><span class="p">,</span> <span class="n">e2</span><span class="p">;</span>

    <span class="cm">/*</span>
<span class="cm">    if(transform(&amp;x0, &amp;y0)||transform(&amp;x1, &amp;y1)){</span>
<span class="cm">        fprintf(stderr, &quot;Transformation ERROR\n&quot;);</span>
<span class="cm">        return -1;</span>
<span class="cm">    }</span>
<span class="cm">    */</span>
    
    <span class="n">transform</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x0</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">y0</span><span class="p">);</span>
    <span class="n">transform</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x1</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">y1</span><span class="p">);</span>

    <span class="n">dx</span> <span class="o">=</span> <span class="n">abs</span><span class="p">(</span><span class="n">x1</span><span class="o">-</span><span class="n">x0</span><span class="p">);</span>
    <span class="n">sx</span> <span class="o">=</span> <span class="n">x0</span><span class="o">&lt;</span><span class="n">x1</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">dy</span> <span class="o">=</span> <span class="n">abs</span><span class="p">(</span><span class="n">y1</span><span class="o">-</span><span class="n">y0</span><span class="p">);</span>
    <span class="n">sy</span> <span class="o">=</span> <span class="n">y0</span><span class="o">&lt;</span><span class="n">y1</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> 
    <span class="n">err</span> <span class="o">=</span> <span class="p">(</span><span class="n">dx</span><span class="o">&gt;</span><span class="n">dy</span> <span class="o">?</span> <span class="n">dx</span> <span class="o">:</span> <span class="o">-</span><span class="n">dy</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span>
    <span class="c1">//e2;</span>
    <span class="c1">//char skipper = 0;</span>

    <span class="c1">//not sure about this yet, but</span>
    <span class="c1">//for the moment trafo is implemented </span>
    <span class="c1">//here</span>

    <span class="k">for</span><span class="p">(;;){</span>
	<span class="c1">//the style shit should become global, </span>
	<span class="c1">//if you do poly s you want continuity etc..</span>
	<span class="c1">//Right??    </span>
        <span class="k">if</span> <span class="p">(</span><span class="n">linestyle</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">skipper</span><span class="o">++</span><span class="p">;</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">skipper</span><span class="o">&lt;</span><span class="mi">5</span><span class="p">)</span> <span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span> <span class="n">y0</span><span class="p">);</span>
            <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">skipper</span><span class="o">==</span><span class="mi">10</span><span class="p">)</span> <span class="n">skipper</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
        
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">linestyle</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">skipper</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">y0</span><span class="p">);</span>
                <span class="n">skipper</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
            <span class="p">}</span><span class="k">else</span> <span class="n">skipper</span><span class="o">++</span><span class="p">;</span>	
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">linestyle</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span> <span class="n">y0</span><span class="p">);</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">x0</span><span class="o">==</span><span class="n">x1</span> <span class="o">&amp;&amp;</span> <span class="n">y0</span><span class="o">==</span><span class="n">y1</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
        <span class="n">e2</span> <span class="o">=</span> <span class="n">err</span><span class="p">;</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">e2</span> <span class="o">&gt;-</span><span class="n">dx</span><span class="p">)</span> <span class="p">{</span> <span class="n">err</span> <span class="o">-=</span> <span class="n">dy</span><span class="p">;</span> <span class="n">x0</span> <span class="o">+=</span> <span class="n">sx</span><span class="p">;</span> <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">e2</span> <span class="o">&lt;</span> <span class="n">dy</span><span class="p">)</span> <span class="p">{</span> <span class="n">err</span> <span class="o">+=</span> <span class="n">dx</span><span class="p">;</span> <span class="n">y0</span> <span class="o">+=</span> <span class="n">sy</span><span class="p">;</span> <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">arc</span><span class="p">(</span><span class="kt">int</span> <span class="n">x0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r1</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r2</span><span class="p">,</span> <span class="kt">int</span> <span class="n">startseg</span><span class="p">,</span> <span class="kt">int</span> <span class="n">endseg</span><span class="p">,</span> <span class="kt">int</span> <span class="n">segs</span><span class="p">)</span> 
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="kt">double</span> <span class="n">alpha</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">x_old</span><span class="p">,</span> <span class="n">y_old</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span>

    <span class="n">alpha</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span> <span class="o">*</span> <span class="n">startseg</span><span class="o">/</span><span class="n">segs</span><span class="p">;</span>
    <span class="n">x_old</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">r1</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span><span class="o">+</span><span class="n">x0</span><span class="p">);</span>
    <span class="n">y_old</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">r2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span><span class="o">+</span><span class="n">y0</span><span class="p">);</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="p">(</span><span class="n">startseg</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span> <span class="n">i</span><span class="o">&lt;=</span><span class="n">endseg</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="n">alpha</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span> <span class="o">*</span> <span class="n">i</span><span class="o">/</span><span class="n">segs</span><span class="p">;</span>
        <span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">r1</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span><span class="o">+</span><span class="n">x0</span><span class="p">);</span>
	<span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">r2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span><span class="o">+</span><span class="n">y0</span><span class="p">);</span>
	<span class="n">line</span><span class="p">(</span><span class="n">x_old</span><span class="p">,</span> <span class="n">y_old</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
	<span class="n">x_old</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>
	<span class="n">y_old</span> <span class="o">=</span> <span class="n">y</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">circle</span><span class="p">(</span><span class="kt">int</span> <span class="n">x0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r1</span><span class="p">,</span> <span class="kt">int</span> <span class="n">segs</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">arc</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span> <span class="n">y0</span><span class="p">,</span> <span class="n">r1</span><span class="p">,</span> <span class="n">r1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">segs</span><span class="p">,</span> <span class="n">segs</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">printxy</span><span class="p">(</span><span class="kt">int</span> <span class="n">x0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y0</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">,</span> <span class="kt">int</span> <span class="n">size_</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">w</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">tx</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">ty</span><span class="p">;</span>
	<span class="k">static</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">mask</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">128</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">};</span>
    <span class="k">struct</span> <span class="n">Color</span> <span class="n">backgrnd</span><span class="p">;</span>

	<span class="k">while</span> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">text</span><span class="o">+</span><span class="n">w</span><span class="p">)</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
		<span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">CHARH</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
			<span class="k">for</span> <span class="p">(</span><span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">CHARW</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
				<span class="k">if</span> <span class="p">(</span><span class="n">CHARMAP</span><span class="p">[</span><span class="n">CHARH</span><span class="o">*</span> <span class="o">*</span><span class="p">(</span><span class="n">text</span><span class="o">+</span><span class="n">w</span><span class="p">)</span> <span class="o">+</span> <span class="n">i</span><span class="p">]</span> <span class="o">&amp;</span> <span class="n">mask</span><span class="p">[</span><span class="n">j</span><span class="p">]){</span>
					<span class="k">if</span> <span class="p">(</span><span class="n">size_</span> <span class="o">==</span> <span class="mi">1</span><span class="p">){</span>
                        <span class="n">tx</span> <span class="o">=</span> <span class="n">x0</span><span class="o">+</span><span class="n">j</span><span class="o">+</span><span class="p">(</span><span class="n">CHARW</span><span class="o">*</span><span class="n">w</span><span class="p">);</span>
                        <span class="n">ty</span> <span class="o">=</span> <span class="n">y0</span><span class="o">+</span><span class="n">i</span><span class="p">;</span>
                        <span class="n">transform</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tx</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ty</span><span class="p">);</span>
						<span class="n">pPlot</span><span class="p">(</span><span class="n">tx</span><span class="p">,</span> <span class="n">ty</span><span class="p">);</span> 
                    <span class="p">}</span>
					<span class="k">else</span><span class="p">{</span>
						<span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">j</span><span class="o">*</span><span class="mi">3</span><span class="o">+</span><span class="p">(</span><span class="n">CHARW</span><span class="o">*</span><span class="n">w</span><span class="o">*</span><span class="mi">3</span><span class="p">),</span> <span class="n">y0</span><span class="o">+</span><span class="p">(</span><span class="n">i</span><span class="o">*</span><span class="mi">3</span><span class="p">));</span>
						<span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">j</span><span class="o">*</span><span class="mi">3</span><span class="o">+</span><span class="p">(</span><span class="n">CHARW</span><span class="o">*</span><span class="n">w</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">y0</span><span class="o">+</span><span class="p">(</span><span class="n">i</span><span class="o">*</span><span class="mi">3</span><span class="p">));</span>
						<span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">j</span><span class="o">*</span><span class="mi">3</span><span class="o">+</span><span class="p">(</span><span class="n">CHARW</span><span class="o">*</span><span class="n">w</span><span class="o">*</span><span class="mi">3</span><span class="p">),</span> <span class="n">y0</span><span class="o">+</span><span class="p">(</span><span class="n">i</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
						<span class="n">pPlot</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">j</span><span class="o">*</span><span class="mi">3</span><span class="o">+</span><span class="p">(</span><span class="n">CHARW</span><span class="o">*</span><span class="n">w</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">y0</span><span class="o">+</span><span class="p">(</span><span class="n">i</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
					<span class="p">}</span>
				<span class="p">}</span>
		<span class="n">w</span><span class="o">++</span><span class="p">;</span>
		<span class="k">if</span> <span class="p">(</span><span class="n">w</span><span class="o">&gt;</span><span class="mi">255</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span><span class="c1">//dont make long sentences...</span>
	<span class="p">}</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">graticule</span><span class="p">(</span><span class="kt">int</span> <span class="n">x0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y0</span><span class="p">,</span> <span class="kt">int</span> <span class="n">w</span><span class="p">,</span> <span class="kt">int</span> <span class="n">h</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">tmpls</span><span class="p">;</span>    <span class="c1">//line style store</span>
	<span class="kt">int</span> <span class="n">dx</span><span class="p">,</span><span class="n">dy</span><span class="p">;</span>
	<span class="kt">double</span> <span class="n">xstep</span><span class="p">;</span>
	<span class="kt">double</span> <span class="n">ystep</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">grid</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>

	<span class="k">struct</span> <span class="n">Color</span> <span class="n">dummycolor</span><span class="p">;</span>
    <span class="n">tmpls</span> <span class="o">=</span> <span class="n">linestyle</span><span class="p">;</span>

	<span class="n">xstep</span> <span class="o">=</span> <span class="mf">1.0</span><span class="o">*</span><span class="n">w</span><span class="o">/</span><span class="n">grid</span><span class="p">;</span>
	<span class="n">ystep</span> <span class="o">=</span> <span class="mf">1.0</span><span class="o">*</span><span class="n">h</span><span class="o">/</span><span class="n">grid</span><span class="p">;</span>	

    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;=</span><span class="n">grid</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
        <span class="n">dx</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">i</span><span class="o">*</span><span class="n">xstep</span><span class="p">);</span>
        <span class="n">dy</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">i</span><span class="o">*</span><span class="n">ystep</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">i</span><span class="o">==</span><span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">linestyle</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
            <span class="c1">//dummycolor = *mycolor2;</span>
        <span class="p">}</span>
        <span class="k">else</span> <span class="p">{</span>
            <span class="n">linestyle</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
            <span class="c1">//dummycolor = *mycolor;</span>
        <span class="p">}</span>
        <span class="c1">//main cross</span>
        <span class="n">line</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">dx</span><span class="p">,</span><span class="n">y0</span><span class="p">,</span><span class="n">x0</span><span class="o">+</span><span class="n">dx</span><span class="p">,</span><span class="n">y0</span><span class="o">+</span><span class="n">h</span><span class="p">);</span>     
        <span class="n">line</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">y0</span><span class="o">+</span><span class="n">dy</span><span class="p">,</span><span class="n">x0</span><span class="o">+</span><span class="n">w</span><span class="p">,</span><span class="n">y0</span><span class="o">+</span><span class="n">dy</span><span class="p">);</span>
        <span class="c1">//ticks</span>
        <span class="n">line</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">dx</span><span class="p">,</span><span class="n">y0</span><span class="o">+</span><span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="mf">4.8</span><span class="o">*</span><span class="n">ystep</span><span class="p">),</span> <span class="n">x0</span><span class="o">+</span><span class="n">dx</span><span class="p">,</span> <span class="n">y0</span><span class="o">+</span><span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="mf">5.2</span><span class="o">*</span><span class="n">ystep</span><span class="p">));</span>
        <span class="n">line</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="mf">4.8</span><span class="o">*</span><span class="n">xstep</span><span class="p">),</span><span class="n">y0</span><span class="o">+</span><span class="n">dy</span><span class="p">,</span><span class="n">x0</span><span class="o">+</span><span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="mf">5.2</span><span class="o">*</span><span class="n">xstep</span><span class="p">),</span><span class="n">y0</span><span class="o">+</span><span class="n">dy</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="n">linestyle</span> <span class="o">=</span> <span class="n">tmpls</span><span class="p">;</span>
    <span class="c1">//currentcolor = </span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">getHeight</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">getWidth</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">xres</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">overlay</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">res_buf</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">sprite_buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">xo</span><span class="p">,</span> <span class="kt">int</span> <span class="n">yo</span><span class="p">,</span> <span class="kt">char</span> <span class="n">sprmode</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">offset</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">offn4</span><span class="p">,</span><span class="n">offo4</span><span class="p">;</span>                <span class="c1">//offn * 4</span>
    <span class="kt">int</span> <span class="n">voffo</span><span class="p">,</span> <span class="n">voffn</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">pix_offset</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">l</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">Oxsb</span><span class="p">,</span> <span class="n">Oysb</span><span class="p">,</span> <span class="n">wsb4</span><span class="p">,</span> <span class="n">hsb</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">LOx</span><span class="p">,</span> <span class="n">LOy</span><span class="p">;</span>               <span class="c1">//local winparams</span>
    <span class="kt">int</span> <span class="n">tailn</span><span class="p">,</span> <span class="n">tailo</span><span class="p">;</span>               <span class="c1">//tail + offset = width</span>
    <span class="kt">int</span> <span class="n">vtailn</span><span class="p">,</span> <span class="n">vtailo</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">tmpL</span><span class="p">;</span>                       <span class="c1">//mod tails</span>
    <span class="kt">int</span> <span class="n">xi</span><span class="p">,</span><span class="n">yi</span><span class="p">;</span>
    <span class="cm">/*</span>
<span class="cm">     * Determine the major square containing sprite @</span>
<span class="cm">     * old position and new position,</span>
<span class="cm">     * I&#39;ll call this me scratchbook sb</span>
<span class="cm">     * Ill copy current fb to sb</span>
<span class="cm">     * restore with res_buf, grab the new res_buf</span>
<span class="cm">     * patch N draw the sprite @ new R in tmp_buf. </span>
<span class="cm">     * finaly copy tmp_buf to fb</span>
<span class="cm">     *</span>
<span class="cm">     * returns -1 if new crds are outabounds</span>
<span class="cm">     * returns -2 if old      ---,,---</span>
<span class="cm">     */</span>

    <span class="cm">/* </span>
<span class="cm">     * and prevent segfaultHELLLLL</span>
<span class="cm">     *</span>
<span class="cm">     * and leave stage gracefully...</span>
<span class="cm">     */</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">-</span><span class="n">Ox</span><span class="o">&gt;=</span><span class="n">width</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="c1">//negative Ox would be &gt; line_length wo</span>
    <span class="c1">//(int)</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">Ox</span><span class="o">&gt;=</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">vinfo</span><span class="p">.</span><span class="n">xres</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">-</span><span class="n">Oy</span><span class="o">&gt;=</span><span class="n">height</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">Oy</span><span class="o">&gt;=</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">-</span><span class="n">xo</span><span class="o">&gt;=</span><span class="n">width</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">xo</span><span class="o">&gt;=</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">vinfo</span><span class="p">.</span><span class="n">xres</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">-</span><span class="n">yo</span><span class="o">&gt;=</span><span class="n">height</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">yo</span><span class="o">&gt;=</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">Ox</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
        <span class="n">LOx</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
        <span class="n">offn4</span> <span class="o">=</span> <span class="n">BPP</span><span class="o">*-</span><span class="n">Ox</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">LOx</span> <span class="o">=</span> <span class="n">Ox</span><span class="p">;</span>
        <span class="n">offn4</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">Oy</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
        <span class="n">LOy</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
        <span class="n">voffn</span> <span class="o">=</span> <span class="o">-</span><span class="n">Oy</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">LOy</span> <span class="o">=</span> <span class="n">Oy</span><span class="p">;</span>
        <span class="n">voffn</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">xo</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
        <span class="n">offo4</span> <span class="o">=</span> <span class="o">-</span><span class="n">BPP</span><span class="o">*</span><span class="n">xo</span><span class="p">;</span>
        <span class="n">xo</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">offo4</span> <span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">yo</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
        <span class="n">voffo</span> <span class="o">=</span> <span class="o">-</span><span class="n">yo</span><span class="p">;</span>
        <span class="n">yo</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">voffo</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">tmpL</span><span class="o">=</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="o">-</span><span class="n">BPP</span><span class="o">*</span><span class="n">xo</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">tmpL</span><span class="o">&lt;</span><span class="n">width4</span><span class="p">)</span>
        <span class="n">tailo</span> <span class="o">=</span> <span class="n">tmpL</span><span class="p">;</span>
    <span class="k">else</span>
        <span class="n">tailo</span> <span class="o">=</span> <span class="n">width4</span><span class="o">-</span><span class="n">offo4</span><span class="p">;</span>

    <span class="n">tmpL</span><span class="o">=</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="o">-</span><span class="n">BPP</span><span class="o">*</span><span class="n">Ox</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">tmpL</span> <span class="o">&lt;</span> <span class="n">width4</span><span class="p">){</span>
        <span class="n">tailn</span> <span class="o">=</span> <span class="n">tmpL</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">tailn</span> <span class="o">=</span> <span class="n">width4</span><span class="o">-</span><span class="n">offn4</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">tmpL</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span> <span class="o">-</span> <span class="n">yo</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">tmpL</span><span class="o">&lt;</span><span class="n">height</span><span class="p">)</span>
        <span class="n">vtailo</span> <span class="o">=</span> <span class="n">tmpL</span><span class="p">;</span>
    <span class="k">else</span>
        <span class="n">vtailo</span> <span class="o">=</span> <span class="n">height</span><span class="o">-</span><span class="n">voffo</span><span class="p">;</span>

    <span class="n">tmpL</span> <span class="o">=</span> <span class="n">vinfo</span><span class="p">.</span><span class="n">yres</span> <span class="o">-</span><span class="n">Oy</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">tmpL</span><span class="o">&lt;</span><span class="n">height</span><span class="p">)</span>
        <span class="n">vtailn</span> <span class="o">=</span> <span class="n">tmpL</span><span class="p">;</span>
    <span class="k">else</span>
        <span class="n">vtailn</span> <span class="o">=</span> <span class="n">height</span><span class="o">-</span><span class="n">voffn</span><span class="p">;</span>    

    <span class="k">if</span> <span class="p">(</span><span class="n">Ox</span><span class="o">&gt;</span><span class="n">xo</span><span class="p">){</span>
        <span class="n">Oxsb</span> <span class="o">=</span> <span class="n">xo</span><span class="p">;</span>
        <span class="n">wsb4</span> <span class="o">=</span> <span class="n">BPP</span><span class="o">*</span><span class="p">(</span><span class="n">LOx</span><span class="o">-</span><span class="n">xo</span><span class="p">)</span><span class="o">+</span><span class="n">tailn</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">Oxsb</span> <span class="o">=</span> <span class="n">LOx</span><span class="p">;</span>
        <span class="n">wsb4</span> <span class="o">=</span> <span class="n">BPP</span><span class="o">*</span><span class="p">(</span><span class="n">xo</span><span class="o">-</span><span class="n">LOx</span><span class="p">)</span><span class="o">+</span><span class="n">tailo</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">Oy</span><span class="o">&gt;</span><span class="n">yo</span><span class="p">){</span>
        <span class="n">Oysb</span> <span class="o">=</span> <span class="n">yo</span><span class="p">;</span>
        <span class="n">hsb</span> <span class="o">=</span> <span class="n">LOy</span><span class="o">-</span><span class="n">yo</span><span class="o">+</span><span class="n">vtailn</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="n">Oysb</span> <span class="o">=</span> <span class="n">LOy</span><span class="p">;</span>
        <span class="n">hsb</span> <span class="o">=</span> <span class="n">yo</span><span class="o">-</span><span class="n">LOy</span><span class="o">+</span><span class="n">vtailo</span><span class="p">;</span>
    <span class="p">}</span>
    
    <span class="cm">/*</span>
<span class="cm">     * Dump current framebuffer to a dummy buffer</span>
<span class="cm">     * then I&#39;ll restore the old sprite patch</span>
<span class="cm">     * here. This will give a pre sprite era</span>
<span class="cm">     * /situation</span>
<span class="cm">     */</span>
    <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">Oxsb</span><span class="o">*</span><span class="n">BPP</span> <span class="o">+</span> <span class="n">Oysb</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
    <span class="n">l</span><span class="o">=</span><span class="n">hsb</span><span class="p">;</span>
    <span class="k">while</span><span class="p">(</span><span class="n">l</span><span class="o">--</span><span class="p">){</span>
        <span class="n">memcpy</span><span class="p">(</span><span class="n">dummy_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">fbp</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">wsb4</span><span class="p">);</span>
        <span class="n">pix_offset</span><span class="o">+=</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* restore dummy (current) frambefuffer</span>
<span class="cm">     * Like no sprite had ever messed up my</span>
<span class="cm">     *background</span>
<span class="cm">     */</span> 

    <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">xo</span><span class="o">*</span><span class="n">BPP</span> <span class="o">+</span> <span class="n">yo</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
    <span class="n">offset</span> <span class="o">=</span> <span class="n">offo4</span><span class="o">+</span><span class="p">(</span><span class="n">voffo</span><span class="o">*</span><span class="n">width4</span><span class="p">);</span>
    <span class="n">l</span><span class="o">=</span><span class="n">vtailo</span><span class="p">;</span>    
    <span class="k">while</span><span class="p">(</span><span class="n">l</span><span class="o">--</span><span class="p">){</span>
        <span class="n">memcpy</span><span class="p">(</span><span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">res_buf</span><span class="o">+</span><span class="n">offset</span><span class="p">,</span> <span class="n">tailo</span><span class="p">);</span>
        <span class="n">memcpy</span><span class="p">(</span><span class="n">dummy_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">res_buf</span><span class="o">+</span><span class="n">offset</span><span class="p">,</span> <span class="n">tailo</span><span class="p">);</span>
        <span class="n">pix_offset</span> <span class="o">+=</span> <span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
        <span class="n">offset</span><span class="o">+=</span><span class="n">width4</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="cm">/* prepare new patch </span>
<span class="cm">     * by grabn new sprite </span>
<span class="cm">     * pos from restored fb</span>
<span class="cm">     * AND</span>
<span class="cm">     * draw new sprite into tmp_buffer</span>
<span class="cm">     */</span>
     
    <span class="k">if</span> <span class="p">(</span><span class="n">sprmode</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span>
        <span class="c1">//TON;</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">yi</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">yi</span><span class="o">&lt;</span><span class="n">vtailn</span><span class="p">;</span><span class="n">yi</span><span class="o">++</span><span class="p">){</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="p">(</span><span class="n">yi</span><span class="o">+</span><span class="n">voffn</span><span class="p">)</span><span class="o">*</span><span class="n">width4</span><span class="o">+</span><span class="n">offn4</span><span class="p">;</span>
            <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">LOx</span><span class="o">*</span><span class="n">BPP</span><span class="o">+</span><span class="p">(</span><span class="n">LOy</span><span class="o">+</span><span class="n">yi</span><span class="p">)</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
            <span class="k">for</span><span class="p">(</span><span class="n">xi</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">xi</span><span class="o">&lt;</span><span class="n">tailn</span><span class="p">;</span><span class="n">xi</span><span class="o">++</span><span class="p">){</span>
               <span class="o">*</span><span class="p">(</span><span class="n">res_buf</span><span class="o">+</span><span class="n">offset</span><span class="p">)</span> <span class="o">=</span> <span class="o">*</span><span class="p">(</span><span class="n">dummy_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">);</span>
                <span class="k">if</span> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">sprite_buf</span><span class="o">+</span><span class="n">offset</span><span class="p">)</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
                    <span class="o">*</span><span class="p">(</span><span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">)</span> <span class="o">=</span> <span class="o">*</span><span class="p">(</span><span class="n">sprite_buf</span><span class="o">+</span><span class="n">offset</span><span class="p">);</span>
                <span class="p">}</span>
                <span class="n">offset</span><span class="o">++</span><span class="p">;</span>
                <span class="n">pix_offset</span><span class="o">++</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="c1">//TOFF;</span>
    <span class="p">}</span>
    
    <span class="c1">//dumps tmp_buff into frambebuffer</span>
    <span class="c1">//only scratchbook size ofcourse</span>
    <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">Oxsb</span><span class="o">*</span><span class="n">BPP</span> <span class="o">+</span> <span class="n">Oysb</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
    <span class="n">l</span><span class="o">=</span><span class="n">hsb</span><span class="p">;</span>
    <span class="n">waitforvsync</span><span class="p">();</span>
    <span class="k">while</span><span class="p">(</span><span class="n">l</span><span class="o">--</span><span class="p">){</span>
        <span class="n">memcpy</span><span class="p">(</span><span class="n">fbp</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">wsb4</span><span class="p">);</span>
        <span class="n">pix_offset</span><span class="o">+=</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">helloworld</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Hello world %i + %i = %i!!</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">mandelbrot</span><span class="p">()</span>
<span class="p">{</span>
    <span class="c1">//just a gimick, </span>
    <span class="c1">//but fun</span>
    

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">read_PNG</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">file_name</span><span class="p">)</span>
<span class="p">{</span>
    <span class="cm">/*</span>
<span class="cm">     * Took this code from doc/fblib</span>
<span class="cm">     * and adjusted it to the likings</span>
<span class="cm">     * of this program.</span>
<span class="cm">     * ?i</span>
<span class="cm">     * It is really awesome that a NOOB</span>
<span class="cm">     * like me, Noisegate can actually</span>
<span class="cm">     * make useable software on an open</span>
<span class="cm">     * system like this Linux...</span>
<span class="cm">     *</span>
<span class="cm">     * openSOFtWARE rocKS</span>
<span class="cm">     */</span>

    <span class="n">png_structp</span> <span class="n">png_ptr</span><span class="p">;</span>
    <span class="n">png_infop</span> <span class="n">info_ptr</span><span class="p">;</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">sig_read</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">png_uint_32</span> <span class="n">_width</span><span class="p">,</span> <span class="n">_height</span><span class="p">,</span> <span class="n">row</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">bit_depth</span><span class="p">,</span> <span class="n">color_type</span><span class="p">,</span> <span class="n">interlace_type</span><span class="p">;</span>
    <span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span><span class="p">;</span>

    <span class="kt">float</span> <span class="n">screen_gamma</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">((</span><span class="n">fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="s">&quot;rb&quot;</span><span class="p">))</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
          <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="cm">/* Create and initialize the png_struct with the desired error handler</span>
<span class="cm">     * functions.  If you want to use the default stderr and longjump method,</span>
<span class="cm">     * you can supply NULL for the last three parameters.  We also supply the</span>
<span class="cm">     * the compiler header file version, so that we know if the application</span>
<span class="cm">     * was compiled with a compatible version of the library.  REQUIRED</span>
<span class="cm">     */</span>
    <span class="c1">//png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, png_voidp user_error_ptr, user_error_fn, user_warning_fn);</span>
    <span class="n">png_ptr</span> <span class="o">=</span> <span class="n">png_create_read_struct</span><span class="p">(</span><span class="n">PNG_LIBPNG_VER_STRING</span><span class="p">,</span> 
            <span class="p">(</span><span class="n">png_voidp</span><span class="p">)</span><span class="nb">NULL</span><span class="p">,</span> 
            <span class="p">(</span><span class="n">png_error_ptr</span><span class="p">)</span><span class="nb">NULL</span><span class="p">,</span> 
            <span class="p">(</span><span class="n">png_error_ptr</span><span class="p">)</span><span class="nb">NULL</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">png_ptr</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">){</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Allocate/initialize the memory for image information.  REQUIRED. */</span>
    <span class="n">info_ptr</span> <span class="o">=</span> <span class="n">png_create_info_struct</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">info_ptr</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
        <span class="n">png_destroy_read_struct</span><span class="p">(</span><span class="o">&amp;</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">png_infopp_NULL</span><span class="p">,</span> <span class="n">png_infopp_NULL</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Set error handling if you are using the setjmp/longjmp method (this is</span>
<span class="cm">    * the normal method of doing things with libpng).  REQUIRED unless you</span>
<span class="cm">    * set up your own error handlers in the png_create_read_struct() earlier.</span>
<span class="cm">    */</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">setjmp</span><span class="p">(</span><span class="n">png_jmpbuf</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">)))</span> <span class="p">{</span>
        <span class="cm">/* Free all of the memory associated with the png_ptr and info_ptr */</span>
        <span class="n">png_destroy_read_struct</span><span class="p">(</span><span class="o">&amp;</span><span class="n">png_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">info_ptr</span><span class="p">,</span> <span class="n">png_infopp_NULL</span><span class="p">);</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
        <span class="cm">/* If we get here, we had a problem reading the file */</span>
        <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error: Couldn&#39;t read the file.&quot;</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* One of the following I/O initialization methods is REQUIRED */</span>
    <span class="cm">/* Set up the input control if you are using standard C streams */</span>
    <span class="n">png_init_io</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">fp</span><span class="p">);</span>

    <span class="cm">/* If we have already read some of the signature */</span>
    <span class="n">png_set_sig_bytes</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">sig_read</span><span class="p">);</span>

   <span class="cm">/* OK, you&#39;re doing it the hard way, with the lower-level functions */</span>

   <span class="cm">/* The call to png_read_info() gives us all of the information from the</span>
<span class="cm">    * PNG file before the first IDAT (image data chunk).  REQUIRED</span>
<span class="cm">    */</span>
   <span class="n">png_read_info</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">);</span>

   <span class="n">png_get_IHDR</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">_width</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">_height</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">bit_depth</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">color_type</span><span class="p">,</span>
       <span class="o">&amp;</span><span class="n">interlace_type</span><span class="p">,</span> <span class="n">int_p_NULL</span><span class="p">,</span> <span class="n">int_p_NULL</span><span class="p">);</span>

   <span class="cm">/* Set up the data transformations you want.  Note that these are all</span>
<span class="cm">    * optional.  Only call them if you want/need them.  Many of the</span>
<span class="cm">    * transformations only work on specific types of images, and many</span>
<span class="cm">    * are mutually exclusive.</span>
<span class="cm">    */</span>

   <span class="cm">/* Tell libpng to strip 16 bit/color files down to 8 bits/color */</span>
   <span class="n">png_set_strip_16</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Strip alpha bytes from the input data without combining with the</span>
<span class="cm">    * background (not recommended).</span>
<span class="cm">    */</span>
   <span class="c1">//png_set_strip_alpha(png_ptr);</span>

   <span class="cm">/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single</span>
<span class="cm">    * byte into separate bytes (useful for paletted and grayscale images).</span>
<span class="cm">    */</span>
   <span class="n">png_set_packing</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Change the order of packed pixels to least significant bit first</span>
<span class="cm">    * (not useful if you are using png_set_packing). */</span>
   <span class="n">png_set_packswap</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Expand paletted colors into true RGB triplets */</span>
   <span class="k">if</span> <span class="p">(</span><span class="n">color_type</span> <span class="o">==</span> <span class="n">PNG_COLOR_TYPE_PALETTE</span><span class="p">)</span>
      <span class="n">png_set_palette_to_rgb</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */</span>
   <span class="k">if</span> <span class="p">(</span><span class="n">color_type</span> <span class="o">==</span> <span class="n">PNG_COLOR_TYPE_GRAY</span> <span class="o">&amp;&amp;</span> <span class="n">bit_depth</span> <span class="o">&lt;</span> <span class="mi">8</span><span class="p">)</span>
      <span class="n">png_set_expand_gray_1_2_4_to_8</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Expand paletted or RGB images with transparency to full alpha channels</span>
<span class="cm">    * so the data will be available as RGBA quartets.</span>
<span class="cm">    */</span>
   <span class="k">if</span> <span class="p">(</span><span class="n">png_get_valid</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="n">PNG_INFO_tRNS</span><span class="p">))</span>
      <span class="n">png_set_tRNS_to_alpha</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Set the background color to draw transparent and alpha images over.</span>
<span class="cm">    * It is possible to set the red, green, and blue components directly</span>
<span class="cm">    * for paletted images instead of supplying a palette index.  Note that</span>
<span class="cm">    * even if the PNG file supplies a background, you are not required to</span>
<span class="cm">    * use it - you should use the (solid) application background if it has one.</span>
<span class="cm">    */</span>

   <span class="n">png_color_16</span> <span class="n">my_background</span><span class="p">,</span> <span class="o">*</span><span class="n">image_background</span><span class="p">;</span>

   <span class="k">if</span> <span class="p">(</span><span class="n">png_get_bKGD</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">image_background</span><span class="p">))</span>
      <span class="n">png_set_background</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">image_background</span><span class="p">,</span>
                         <span class="n">PNG_BACKGROUND_GAMMA_FILE</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">);</span>
   <span class="k">else</span>
      <span class="n">png_set_background</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">my_background</span><span class="p">,</span>
                         <span class="n">PNG_BACKGROUND_GAMMA_SCREEN</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">);</span>


   <span class="n">screen_gamma</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">;</span>

   <span class="kt">int</span> <span class="n">intent</span><span class="p">;</span>

   <span class="k">if</span> <span class="p">(</span><span class="n">png_get_sRGB</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">intent</span><span class="p">))</span>
      <span class="n">png_set_gamma</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">screen_gamma</span><span class="p">,</span> <span class="mf">0.45455</span><span class="p">);</span>
   <span class="k">else</span>
   <span class="p">{</span>
      <span class="kt">double</span> <span class="n">image_gamma</span><span class="p">;</span>
      <span class="k">if</span> <span class="p">(</span><span class="n">png_get_gAMA</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">image_gamma</span><span class="p">))</span>
         <span class="n">png_set_gamma</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">screen_gamma</span><span class="p">,</span> <span class="n">image_gamma</span><span class="p">);</span>
      <span class="k">else</span>
         <span class="n">png_set_gamma</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">screen_gamma</span><span class="p">,</span> <span class="mf">0.45455</span><span class="p">);</span>
   <span class="p">}</span>

   <span class="cm">/* Invert monochrome files to have 0 as white and 1 as black */</span>
   <span class="n">png_set_invert_mono</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* If you want to shift the pixel values from the range [0,255] or</span>
<span class="cm">    * [0,65535] to the original [0,7] or [0,31], or whatever range the</span>
<span class="cm">    * colors were originally in:</span>
<span class="cm">    */</span>
   <span class="k">if</span> <span class="p">(</span><span class="n">png_get_valid</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="n">PNG_INFO_sBIT</span><span class="p">))</span>
   <span class="p">{</span>
      <span class="n">png_color_8p</span> <span class="n">sig_bit_p</span><span class="p">;</span>

      <span class="n">png_get_sBIT</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">sig_bit_p</span><span class="p">);</span>
      <span class="n">png_set_shift</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">sig_bit_p</span><span class="p">);</span>
   <span class="p">}</span>

   <span class="cm">/* Flip the RGB pixels to BGR (or RGBA to BGRA) */</span>
   <span class="k">if</span> <span class="p">(</span><span class="n">color_type</span> <span class="o">&amp;</span> <span class="n">PNG_COLOR_MASK_COLOR</span><span class="p">)</span>
      <span class="n">png_set_bgr</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */</span>
   <span class="n">png_set_swap_alpha</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Swap bytes of 16 bit files to least significant byte first */</span>
   <span class="n">png_set_swap</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

   <span class="cm">/* Add filler (or alpha) byte (before/after each RGB triplet) */</span>
   <span class="n">png_set_filler</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="mh">0x00</span><span class="p">,</span> <span class="n">PNG_FILLER_AFTER</span><span class="p">);</span>

   <span class="cm">/* Turn on interlace handling.  REQUIRED if you are not using</span>
<span class="cm">    * png_read_image().  To see how to handle interlacing passes,</span>
<span class="cm">    * see the png_read_row() method below:</span>
<span class="cm">    */</span>
   <span class="c1">//number_passes = png_set_interlace_handling(png_ptr);</span>

   <span class="cm">/* Optional call to gamma correct and add the background to the palette</span>
<span class="cm">    * and update info structure.  REQUIRED if you are expecting libpng to</span>
<span class="cm">    * update the palette for you (ie you selected such a transform above).</span>
<span class="cm">    */</span>
   <span class="n">png_read_update_info</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">);</span>

   <span class="cm">/* Allocate the memory to hold the image using the fields of info_ptr. */</span>

   <span class="cm">/* The easiest way to read the image: */</span>
   <span class="n">png_bytep</span> <span class="n">row_pointers</span><span class="p">[</span><span class="n">_height</span><span class="p">];</span>

   <span class="cm">/* Clear the pointer array */</span>
   <span class="k">for</span> <span class="p">(</span><span class="n">row</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">row</span> <span class="o">&lt;</span> <span class="n">_height</span><span class="p">;</span> <span class="n">row</span><span class="o">++</span><span class="p">)</span>
      <span class="n">row_pointers</span><span class="p">[</span><span class="n">row</span><span class="p">]</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>

   <span class="k">for</span> <span class="p">(</span><span class="n">row</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">row</span> <span class="o">&lt;</span> <span class="n">_height</span><span class="p">;</span> <span class="n">row</span><span class="o">++</span><span class="p">)</span>
      <span class="n">row_pointers</span><span class="p">[</span><span class="n">row</span><span class="p">]</span> <span class="o">=</span> <span class="n">png_malloc</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">png_get_rowbytes</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span>
         <span class="n">info_ptr</span><span class="p">));</span>

   <span class="cm">/* Now it&#39;s time to read the image.  One of these methods is REQUIRED */</span>
   <span class="n">png_read_image</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">row_pointers</span><span class="p">);</span>


   <span class="cm">/* Now mess this rowpointer thing into the tmp_buf</span>
<span class="cm">    * U dont know if width&gt;_width and height&gt;_height</span>
<span class="cm">    * so we trunc the guy</span>
<span class="cm">    */</span>
    
   <span class="kt">int</span> <span class="n">maxY</span> <span class="o">=</span> <span class="n">_height</span><span class="o">&gt;</span><span class="n">height</span><span class="o">?</span><span class="n">height</span><span class="o">:</span><span class="n">_height</span><span class="p">;</span>
   <span class="kt">int</span> <span class="n">maxX</span> <span class="o">=</span> <span class="n">_width</span><span class="o">&gt;</span><span class="n">width</span><span class="o">?</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="o">:</span><span class="n">_width</span><span class="p">;</span>
   <span class="kt">int</span> <span class="n">pix_offset</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>

   <span class="k">for</span> <span class="p">(</span><span class="n">row</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">row</span><span class="o">&lt;</span><span class="n">maxY</span><span class="p">;</span><span class="n">row</span><span class="o">++</span><span class="p">){</span>
       <span class="n">pix_offset</span> <span class="o">=</span> <span class="n">Ox</span><span class="o">*</span><span class="n">BPP</span> <span class="o">+</span> <span class="p">((</span><span class="n">Oy</span><span class="o">+</span><span class="n">row</span><span class="p">)</span><span class="o">*</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="p">);</span>
       <span class="n">memcpy</span><span class="p">(</span><span class="n">tmp_buf</span><span class="o">+</span><span class="n">pix_offset</span><span class="p">,</span> <span class="n">row_pointers</span><span class="p">[</span><span class="n">row</span><span class="p">],</span> <span class="n">maxX</span><span class="o">*</span><span class="n">BPP</span><span class="p">);</span>
   <span class="p">}</span>

   <span class="cm">/* Read rest of file, and get additional chunks in info_ptr - REQUIRED */</span>
   <span class="n">png_read_end</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">);</span>

   <span class="cm">/* At this point you have read the entire image */</span>

   <span class="cm">/* Clean up after the read, and free any memory allocated - REQUIRED */</span>
   <span class="n">png_destroy_read_struct</span><span class="p">(</span><span class="o">&amp;</span><span class="n">png_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">info_ptr</span><span class="p">,</span> <span class="n">png_infopp_NULL</span><span class="p">);</span>

   <span class="cm">/* Close the file */</span>
   <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>


   <span class="c1">//printf(&quot;png W x H = %i x %i\n&quot;, _width, _height);</span>
   <span class="cm">/* That&#39;s it */</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>


<span class="kt">int</span> <span class="nf">write_PNG</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">,</span> <span class="kt">int</span> <span class="n">interlace</span><span class="p">,</span> <span class="kt">char</span> <span class="n">borfb</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">//hardcore theft from fbgrab.c</span>
	<span class="cm">/*</span>
<span class="cm">	 * fbgrab - takes screenshots using the framebuffer.</span>
<span class="cm">	 *</span>
<span class="cm">	 * (C) Gunnar Monell &lt;gmo@linux.nu&gt; 2002</span>
<span class="cm">	 *</span>
<span class="cm">	 * This program is free Software, see the COPYING file</span>
<span class="cm">	 * and is based on Stephan Beyer&#39;s &lt;fbshot@s-beyer.de&gt; FBShot</span>
<span class="cm">	 * (C) 2000.</span>
<span class="cm">	 *</span>
<span class="cm">	 * For features and differences, read the manual page. </span>
<span class="cm">	 *</span>
<span class="cm">	 * This program has been checked with &quot;splint +posixlib&quot; without</span>
<span class="cm">	 * warnings. Splint is available from http://www.splint.org/ .</span>
<span class="cm">	 * Patches and enhancements of fbgrab have to fulfill this too.</span>
<span class="cm">	 */</span>

    <span class="n">png_uint_32</span> <span class="n">i</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">bit_depth</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">color_type</span><span class="p">;</span>
    <span class="n">png_uint_32</span> <span class="n">uheight</span><span class="p">,</span> <span class="n">uwidth</span><span class="p">;</span>

    <span class="n">uheight</span> <span class="o">=</span> <span class="p">(</span><span class="n">png_uint_32</span><span class="p">)</span><span class="n">height</span><span class="p">;</span>
    <span class="n">uwidth</span> <span class="o">=</span> <span class="p">(</span><span class="n">png_uint_32</span><span class="p">)</span><span class="n">width</span><span class="p">;</span>

    <span class="n">png_bytep</span> <span class="n">row_pointers</span><span class="p">[</span><span class="n">uheight</span><span class="p">];</span>
    <span class="n">png_structp</span> <span class="n">png_ptr</span><span class="p">;</span>
    <span class="n">png_infop</span> <span class="n">info_ptr</span><span class="p">;</span>
    <span class="kt">FILE</span> <span class="o">*</span><span class="n">outfile</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&quot;wb&quot;</span><span class="p">);</span>

    <span class="n">interlace</span> <span class="o">=</span> <span class="n">PNG_INTERLACE_NONE</span><span class="p">;</span>    

    <span class="c1">//printf (&quot;%d, %d\n&quot;, uheight, uwidth);</span>

    <span class="c1">//well, either from the buffer or the framebuffer...</span>
    <span class="c1">//yih, gotta clean this one up, reallyyyyy</span>
    <span class="c1">//</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">borfb</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">uheight</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
	        <span class="n">row_pointers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">fbp</span> <span class="o">+</span> <span class="p">(</span><span class="n">BPP</span><span class="o">*</span><span class="n">Ox</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">Oy</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1</span> <span class="o">*</span> <span class="p">(</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="cm">/*uwidth+10*/</span><span class="p">));</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">borfb</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">uheight</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
	        <span class="n">row_pointers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)(</span><span class="n">tmp_buf</span> <span class="o">+</span> <span class="p">(</span><span class="n">BPP</span><span class="o">*</span><span class="n">Ox</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">Oy</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1</span> <span class="o">*</span> <span class="p">(</span><span class="n">finfo</span><span class="p">.</span><span class="n">line_length</span><span class="cm">/*uwidth+10*/</span><span class="p">));</span>
 
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">outfile</span><span class="p">){</span> 
	    <span class="n">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error: Couldn&#39;t fopen %s.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">filename</span><span class="p">);</span>
	    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    
    <span class="n">png_ptr</span> <span class="o">=</span> <span class="n">png_create_write_struct</span><span class="p">(</span><span class="n">PNG_LIBPNG_VER_STRING</span><span class="p">,</span>
        <span class="p">(</span><span class="n">png_voidp</span><span class="p">)</span> <span class="nb">NULL</span><span class="p">,</span> <span class="p">(</span><span class="n">png_error_ptr</span><span class="p">)</span> <span class="nb">NULL</span><span class="p">,</span> <span class="p">(</span><span class="n">png_error_ptr</span><span class="p">)</span> <span class="nb">NULL</span><span class="p">);</span>
    
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">png_ptr</span><span class="p">){</span>
	    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;Error: Couldn&#39;t create PNG write struct.&quot;</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">info_ptr</span> <span class="o">=</span> <span class="n">png_create_info_struct</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">info_ptr</span><span class="p">){</span>
	    <span class="n">png_destroy_write_struct</span><span class="p">(</span><span class="o">&amp;</span><span class="n">png_ptr</span><span class="p">,</span> <span class="p">(</span><span class="n">png_infopp</span><span class="p">)</span> <span class="nb">NULL</span><span class="p">);</span>
	    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;Error: Couldn&#39;t create PNG info struct.&quot;</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">setjmp</span><span class="p">(</span><span class="n">png_jmpbuf</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">))){</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">outfile</span><span class="p">);</span>
        <span class="n">png_destroy_write_struct</span><span class="p">(</span><span class="o">&amp;</span><span class="n">png_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">info_ptr</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="p">;</span>
    <span class="p">}</span>
 
    <span class="n">png_init_io</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">outfile</span><span class="p">);</span>
    
    <span class="c1">//png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);</span>
    
    <span class="n">bit_depth</span> <span class="o">=</span> <span class="mi">8</span><span class="p">;</span>
    <span class="n">color_type</span> <span class="o">=</span> <span class="n">PNG_COLOR_TYPE_RGB_ALPHA</span><span class="p">;</span>
    
    <span class="n">png_set_IHDR</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">,</span> <span class="n">uwidth</span><span class="p">,</span> <span class="n">uheight</span><span class="p">,</span> 
		 <span class="n">bit_depth</span><span class="p">,</span> <span class="n">PNG_COLOR_TYPE_RGBA</span><span class="p">,</span> <span class="n">PNG_INTERLACE_NONE</span><span class="p">,</span> 
		 <span class="n">PNG_COMPRESSION_TYPE_DEFAULT</span><span class="p">,</span> <span class="n">PNG_FILTER_TYPE_DEFAULT</span><span class="p">);</span>

    <span class="c1">//png_set_packing(png_ptr);</span>
    <span class="n">png_set_invert_alpha</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>
    <span class="c1">//</span>
    <span class="c1">//png_set function must be after set_IHDR</span>
    <span class="c1">//according to libpng-manual</span>
    <span class="n">png_set_filter</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">PNG_FILTER_NONE</span><span class="p">);</span>
    <span class="n">png_set_filler</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="mi">0</span> <span class="p">,</span> <span class="n">PNG_FILLER_BEFORE</span><span class="p">);</span>
    <span class="n">png_set_bgr</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">);</span>
    
    <span class="n">png_write_info</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">);</span>
    
    <span class="c1">//printf (&quot;Now writing PNG file\n&quot;);</span>
    <span class="n">png_write_image</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">row_pointers</span><span class="p">);</span>
    <span class="c1">//png_write_rows(png_ptr, row_pointers, uheight);</span>
    <span class="n">png_write_end</span><span class="p">(</span><span class="n">png_ptr</span><span class="p">,</span> <span class="n">info_ptr</span><span class="p">);</span>
    <span class="c1">//puh, done, now freeing memory... </span>
    <span class="n">png_destroy_write_struct</span><span class="p">(</span><span class="o">&amp;</span><span class="n">png_ptr</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">info_ptr</span><span class="p">);</span>
    
    <span class="k">if</span> <span class="p">(</span><span class="n">outfile</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
	    <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">fclose</span><span class="p">(</span><span class="n">outfile</span><span class="p">);</span>
    <span class="c1">//printf(&quot;Done writing...\n&quot;);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/*</span>
<span class="cm"> * TODO:</span>
<span class="cm">    double Sx[3][3];</span>
<span class="cm">    double Sy[3][3];</span>

<span class="cm">    Sx[0][0] = 1.0;</span>
<span class="cm">    Sx[0][1] = 0.0;</span>
<span class="cm">    Sx[0][2] = 0.0;</span>
<span class="cm">    Sx[1][0] = 0.0;</span>
<span class="cm">    Sx[1][1] = 1.0;</span>
<span class="cm">    Sx[1][2] = 0.0;</span>
<span class="cm">    Sx[2][0] = -tan(-angle/2.0);</span>
<span class="cm">    Sx[2][1] = 0.0;</span>
<span class="cm">    Sx[2][2] = 1.0;</span>

<span class="cm">    Sy[0][0] = 1.0;</span>
<span class="cm">    Sy[0][1] = 0.0;</span>
<span class="cm">    Sy[0][2] = sin(-angle);</span>
<span class="cm">    Sy[1][0] = 0.0;</span>
<span class="cm">    Sy[1][1] = 1.0;</span>
<span class="cm">    Sy[1][2] = 0.0;</span>
<span class="cm">    Sy[2][0] = 0.0;</span>
<span class="cm">    Sy[2][1] = 0.0;</span>
<span class="cm">    Sy[2][2] = 1.0;</span>

<span class="cm">    matrixvec3(Sx, x, y, z);</span>
<span class="cm">    matrixvec3(Sy, x, y, z);</span>
<span class="cm">    matrixvec3(Sx, x, y, z);    </span>
<span class="cm"> */</span>
</pre></div>
</body>
</html>
